Fix asyncDelegator reporting "done" too early #51274
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #45400
I wasn't entirely sure where I should be adding unit tests for a fix like this one so would really appreciate some guidance in that area.
Regarding the fix itself, let me try to explain why I believe the previous code was incorrect. In the following snippet:
the
iterator
returned by__asyncDelegator
reportsdone = true
one step too early wheniterator.return()
is called. Instead it should emit{ value: __await(o[n](v)), done: false }
and wait until the correlated__asyncGenerator
will calliterator.next(...)
with theresult
of the promise wrapped with__await
, sayresult = { value, done }
, obtained from the inner generator viao.return(v)
. Sinceiterator
is alternating it's behavior on each call (because ofp = !p
),iterator.next
will return{ value, done }
explicitly this time and ifresult.done
happens to betrue
, then the processing will stop as expected. On the other hand, ifresult.done = false
, e.g. when there is ayield
infinally
block, the processing will continue untildone = true
is eventually observed.