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

Make async iterator return() and next() more like async generators #891

Merged
merged 3 commits into from
Jun 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
63 changes: 38 additions & 25 deletions index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -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|.
Copy link
Member

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.

try {
  const a = await it.next();  // eventually rejected
} catch {
  const b = await it.next();
}

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:

const a = it.next();  // note: no await here
const b = it.next();

It seems like b should be settled in the same way as b above, as the timing of when it.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.

Copy link
Member Author

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).

Copy link
Member Author

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.

Copy link
Member Author

@domenic domenic Jun 5, 2020

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)

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>
Expand Down Expand Up @@ -12599,31 +12605,38 @@ 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$](|value|, <emu-val>true</emu-val>).
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]].
Expand Down