Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs.createReadStream(...)[Symbol.asyncIterator]() is not async iterable #23041

Closed
alanshaw opened this issue Sep 23, 2018 · 1 comment
Closed
Assignees
Labels
confirmed-bug Issues with confirmed bugs. experimental Issues and PRs related to experimental features. stream Issues and PRs related to the stream subsystem.

Comments

@alanshaw
Copy link

  • Version: v10.10.0
  • Platform: n/a
  • Subsystem: ?
const fs = require('fs')

async function main () {
  const iterator = fs.createReadStream(__filename)[Symbol.asyncIterator]()
  for await (const chunk of iterator) {
    console.log('got a chunk', chunk.toString())
  }
}

main()

I get the error:

(node:13875) UnhandledPromiseRejectionWarning: TypeError: iterator is not async iterable

This isn't exactly a bug, but I think the iterator returned by fs.createReadStream(__filename)[Symbol.asyncIterator]() doesn't implement the iterable protocol which is why this is happening.

It is not possible to know reflectively whether a particular object implements the iterator protocol, however it is easy to create an object that satisfies both the iterator and iterable protocols (as shown in the example below). Doing so allows an iterator to be consumed by the various syntaxes expecting iterables. Thus it is rarely desireable to implement the iterator protocol without also implementing iterable.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol

This might be useful if you wanted to consume the first X chunks of a stream using await iterator.next() and then conditionally consume the rest with for await of.

@devsnek devsnek added fs Issues and PRs related to the fs subsystem / file system. stream Issues and PRs related to the stream subsystem. experimental Issues and PRs related to experimental features. confirmed-bug Issues with confirmed bugs. and removed fs Issues and PRs related to the fs subsystem / file system. labels Sep 23, 2018
@devsnek devsnek self-assigned this Sep 23, 2018
@devsnek
Copy link
Member

devsnek commented Sep 23, 2018

fixed locally... just running tests and then i'll have a pr up

devsnek added a commit to devsnek/node that referenced this issue Oct 9, 2018
Closes: nodejs#23041

- Rewrite `ReadableAsyncIterator` class into
`ReadableStreamAsyncIteratorPrototype` which contains no constructor and
inherits from `%AsyncIteratorPrototype%`.

- Rewrite `AsyncIteratorRecord` into dumb function.

PR-URL: nodejs#23042
Fixes: nodejs#23041
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
targos pushed a commit that referenced this issue Oct 10, 2018
Closes: #23041

- Rewrite `ReadableAsyncIterator` class into
`ReadableStreamAsyncIteratorPrototype` which contains no constructor and
inherits from `%AsyncIteratorPrototype%`.

- Rewrite `AsyncIteratorRecord` into dumb function.

PR-URL: #23042
Fixes: #23041
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
jasnell pushed a commit that referenced this issue Oct 17, 2018
Closes: #23041

- Rewrite `ReadableAsyncIterator` class into
`ReadableStreamAsyncIteratorPrototype` which contains no constructor and
inherits from `%AsyncIteratorPrototype%`.

- Rewrite `AsyncIteratorRecord` into dumb function.

PR-URL: #23042
Fixes: #23041
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. experimental Issues and PRs related to experimental features. stream Issues and PRs related to the stream subsystem.
Projects
None yet
Development

No branches or pull requests

2 participants