-
Notifications
You must be signed in to change notification settings - Fork 157
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
Make async iterator return() and next() more like async generators #891
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12543,23 +12543,29 @@ The \[[Prototype]] [=internal slot=] of an [=asynchronous iterator prototype obj | |
1. Let |value| be |next|, [=converted to an ECMAScript value=]. | ||
1. Return [=!=] [$CreateIterResultObject$](|value|, <emu-val>false</emu-val>). | ||
1. Let |onFulfilled| be [=!=] [$CreateBuiltinFunction$](|fulfillSteps|, « »). | ||
1. Perform [=!=] [$PerformPromiseThen$](|nextPromise|, |onFulfilled|, | ||
<emu-val>undefined</emu-val>, |nextPromiseCapability|). | ||
1. Let |rejectSteps| be the following steps, given |reason|: | ||
1. Set |object|'s [=default asynchronous iterator object/ongoing promise=] to | ||
null. | ||
1. Set |object|'s [=default asynchronous iterator object/is finished=] to true. | ||
1. [=ECMAScript/Throw=] |reason|. | ||
1. Let |onRejected| be [=!=] [$CreateBuiltinFunction$](|rejectSteps|, « »). | ||
1. Perform [=!=] [$PerformPromiseThen$](|nextPromise|, |onFulfilled|, |onRejected|, | ||
|nextPromiseCapability|). | ||
1. Return |nextPromiseCapability|.\[[Promise]]. | ||
|
||
1. Let |promise| be |object|'s [=default asynchronous iterator object/ongoing promise=]. | ||
1. Let |ongoingPromise| be |object|'s [=default asynchronous iterator object/ongoing promise=]. | ||
|
||
1. If |promise| is not null, then: | ||
1. If |ongoingPromise| is not null, then: | ||
1. Let |afterOngoingPromiseCapability| be [=!=] [$NewPromiseCapability$]({{%Promise%}}). | ||
1. Let |onFulfilled| be [=!=] [$CreateBuiltinFunction$](|nextSteps|, « »). | ||
1. Perform [=!=] [$PerformPromiseThen$](|promise|, |onFulfilled|, | ||
<emu-val>undefined</emu-val>, |afterOngoingPromiseCapability|). | ||
1. Let |onSettled| be [=!=] [$CreateBuiltinFunction$](|nextSteps|, « »). | ||
1. Perform [=!=] [$PerformPromiseThen$](|ongoingPromise|, |onSettled|, |onSettled|, | ||
|afterOngoingPromiseCapability|). | ||
1. Set |object|'s [=default asynchronous iterator object/ongoing promise=] to | ||
|afterOngoingPromiseCapability|.\[[Promise]]. | ||
|
||
1. Otherwise: | ||
1. Run |nextSteps| and set |object|'s | ||
[=default asynchronous iterator object/ongoing promise=] to the result. | ||
1. Set |object|'s [=default asynchronous iterator object/ongoing promise=] to the result of | ||
running |nextSteps|. | ||
|
||
1. Return |object|'s [=default asynchronous iterator object/ongoing promise=]. | ||
</div> | ||
|
@@ -12599,31 +12605,39 @@ The \[[Prototype]] [=internal slot=] of an [=asynchronous iterator prototype obj | |
<emu-val>undefined</emu-val>, « |error| »). | ||
1. Return |returnPromiseCapability|.\[[Promise]]. | ||
|
||
1. If |object|'s [=default asynchronous iterator object/ongoing promise=] is not null, then: | ||
1. Let |error| be a new {{ECMAScript/TypeError}}. | ||
1. Perform [=!=] [$Call$](|returnPromiseCapability|.\[[Reject]], | ||
<emu-val>undefined</emu-val>, « |error| »). | ||
1. Return |returnPromiseCapability|.\[[Promise]]. | ||
1. Let |returnSteps| be the following steps: | ||
1. Let |returnPromiseCapability| be [=!=] [$NewPromiseCapability$]({{%Promise%}}). | ||
1. If |object|'s [=default asynchronous iterator object/is finished=] is true, then: | ||
1. Let |result| be [$CreateIterResultObject$](<emu-val>undefined</emu-val>, | ||
<emu-val>true</emu-val>). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The webidl2js implementation uses value as the first parameter rather than undefined, I believe correctly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yep, and that's what async generators do. Great catch; thank you! |
||
1. Perform [=!=] [$Call$](|returnPromiseCapability|.\[[Resolve]], | ||
<emu-val>undefined</emu-val>, « |result| »). | ||
1. Return |returnPromiseCapability|.\[[Promise]]. | ||
1. Set |object|'s [=default asynchronous iterator object/is finished=] to true. | ||
1. Return the result of running the [=asynchronous iterator return=] algorithm for | ||
|interface|, given |object|'s [=default asynchronous iterator object/target=], |object|, | ||
and |value|. | ||
|
||
1. If |object|'s [=default asynchronous iterator object/is finished=] is true, then: | ||
1. Let |result| be [=!=] [$CreateIterResultObject$](|value|, | ||
<emu-val>true</emu-val>). | ||
1. Perform [=!=] [$Call$](|returnPromiseCapability|.\[[Resolve]], | ||
<emu-val>undefined</emu-val>, « |result| »). | ||
1. Return |returnPromiseCapability|.\[[Promise]]. | ||
1. Let |returnStepsPromise| be null. | ||
|
||
1. Set |object|'s [=default asynchronous iterator object/is finished=] to true. | ||
1. Let |ongoingPromise| be |object|'s [=default asynchronous iterator object/ongoing promise=]. | ||
|
||
1. Let |returnPromise| be the result of running the [=asynchronous iterator return=] algorithm | ||
for |interface|, given |object|'s [=default asynchronous iterator object/target=], | ||
|object|, and |value|. | ||
1. If |ongoingPromise| is not null, then: | ||
1. Let |afterOngoingPromiseCapability| be [=!=] [$NewPromiseCapability$]({{%Promise%}}). | ||
1. Let |onSettled| be [=!=] [$CreateBuiltinFunction$](|returnSteps|, « »). | ||
1. Perform [=!=] [$PerformPromiseThen$](|ongoingPromise|, |onSettled|, |onSettled|, | ||
|afterOngoingPromiseCapability|). | ||
1. Set |returnStepsPromise| to |afterOngoingPromiseCapability|.\[[Promise]]. | ||
|
||
1. Otherwise: | ||
1. Set |returnStepsPromise| to the result of running |returnSteps|. | ||
|
||
1. Let |fulfillSteps| be the following steps: | ||
1. Return [=!=] [$CreateIterResultObject$](|value|, <emu-val>true</emu-val>). | ||
|
||
1. Let |onFulfilled| be [=!=] [$CreateBuiltinFunction$](|fulfillSteps|, « »). | ||
|
||
1. Perform [=!=] [$PerformPromiseThen$](|returnPromise|, |onFulfilled|, | ||
1. Perform [=!=] [$PerformPromiseThen$](|returnStepsPromise|, |onFulfilled|, | ||
<emu-val>undefined</emu-val>, |returnPromiseCapability|). | ||
|
||
1. Return |returnPromiseCapability|.\[[Promise]]. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few more things to think about.
Async generators (and also sync generators) would set
b
to{ value: undefined, done: true }
. We could do the same by just setting the is finished flag to true, which would give us the invariant that getting the next iteration result will never be called after it has rejected a promise, which is something I think spec authors could come to expect.Alternatively, we could consider a “one bad apple spoils the bunch” semantic, where
b
gets rejected. I think we should stick with the async generator behavior though.Going further, consider this case:
It seems like
b
should be settled in the same way asb
above, as the timing of whenit.next()
is called shouldn't have any bearing on its result. But then this would require us to keep some unsettled promise set on object's target, which brings about additional complexity. Async generators has something analogous through the [[AsyncGeneratorQueue]] internal slot; see spec.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like these ideas, and I will work to align the spec with generators in the way you suggest. It's good that we're getting this review coverage now that these things are being used seriously in specs, and implemented (albeit only implemented in webidl2js so far).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've attempted to implement semantics that pass these test cases in the latest commit of jsdom/webidl2js#224. If you agree that the changes there are reasonable, I'll port them over to spec text form.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also feel free to peruse the tests added in web-platform-tests/wpt#22982 (commits)