Fix asyncDelegator reporting "done" too early#51274
Conversation
|
To test this you could add an evaluation test in src/testRunner/unittests/evaluation/asyncGenerator.ts |
|
The This looks like the correct fix, so I can approve and merge if you're willing to add an evaluation test. We'll need to ensure the |
|
@rbuckton Thank you for looking into this PR and for pointing me to 😄 As requested, added a unit test based on the example from my original issue report and I also created a parallel pull request here: |
|
@rbuckton Thank you for merging this! This one is of a particular importance for my team at the moment, so it's much much appreciated 😄 |
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
iteratorreturned by__asyncDelegatorreportsdone = trueone step too early wheniterator.return()is called. Instead it should emit{ value: __await(o[n](v)), done: false }and wait until the correlated__asyncGeneratorwill calliterator.next(...)with theresultof the promise wrapped with__await, sayresult = { value, done }, obtained from the inner generator viao.return(v). Sinceiteratoris alternating it's behavior on each call (because ofp = !p),iterator.nextwill return{ value, done }explicitly this time and ifresult.donehappens to betrue, then the processing will stop as expected. On the other hand, ifresult.done = false, e.g. when there is ayieldinfinallyblock, the processing will continue untildone = trueis eventually observed.