next 1 resolved with { value: 'a', done: false }
cancelled
next 2 resolved with { done: true, value: undefined }
return resolved with { done: true, value: undefined }
This is quite surprising: the second next() promise resolves before the return() promise, even though it was called afterreturn(). Intuitively, we would expect all async iterator calls to get queued. Indeed, if you do the same thing with an async generator:
then the promises resolve in the order of the original calls:
next 1 resolved with { value: 'a', done: false }
cancelled
return resolved with { value: undefined, done: true }
next 2 resolved with { value: undefined, done: true }
Yuki and I believe this to be a bug in the Web IDL specification. More precisely, the return() method should update the ongoing promise, such that future calls to next() and return() are properly chained. This aligns more closely with the behavior of async generators.
Suggested commit message:
Update ongoing promise in async iterator return() method
This fixes an edge case where manually calling return(); next() could result in the next() promise resolving before the return() promise.
[ ] At least two implementers are interested (and none opposed):
Chromium (@yuki3)
…
[x] Tests are written and can be reviewed and commented upon at:
@yuki3 found an interesting edge case related to async iterators.
In Firefox and Node.js (where async iteration on
ReadableStream
is already supported), when you run this snippet:you get:
This is quite surprising: the second
next()
promise resolves before thereturn()
promise, even though it was called afterreturn()
. Intuitively, we would expect all async iterator calls to get queued. Indeed, if you do the same thing with an async generator:then the promises resolve in the order of the original calls:
Yuki and I believe this to be a bug in the Web IDL specification. More precisely, the
return()
method should update the ongoing promise, such that future calls tonext()
andreturn()
are properly chained. This aligns more closely with the behavior of async generators.Suggested commit message:
ReadableStream.prototype[Symbol.asyncIterator]
, which WebKit doesn't support yet. (https://bugs.webkit.org/show_bug.cgi?id=194379)(See WHATWG Working Mode: Changes for more details.)
Preview | Diff