Skip to content

finally is not called when asynchronously iterating over synchronous generator which yields rejected promise #1849

@vadzim

Description

@vadzim

Description: Finally block in sync generator in try-finally statement is ignored and is not executed if that generator yields rejected promise and is iterated over with for-await-of loop.

void async function() {
	try {
		for await (const x of function*() {
			try { yield Promise.reject("reject") }
			finally { console.log("finally after reject") }
		}()) {
			console.log(await x)
		}
	} catch (e) {
		console.log(e)
	}
}()

eshost Output:

$ eshost -s -x 'void async function() {
	try {
		for await (const x of function*() {
			try { yield Promise.reject("reject") }
			finally { console.log("finally after reject") }
		}()) {
			console.log(await x)
		}
	} catch (e) {
		console.log(e)
	}
}()'
#### sm, v8
reject

I expect the following output:

finally after reject
reject

If the generator is async then finally block is executed as expected.

If for-await-of loop is transpiled with babel then finally block is executed as expected.

There is a comment about that bug and spec compliance in firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1610315#c1

Update.
This bug causes the next two code fragments to behave differently:
1.

   for (const _x of function*() {
      try { yield Promise.reject("reject") }
      finally { console.log("finally after reject") }
   }()){
      const x = await _x
      ....
   }
   for await (const x of function*() {
      try { yield Promise.reject("reject") }
      finally { console.log("finally after reject") }
   }()){
      ...
   }

The first loop executes finally block, but the second one doesn't, though both of them resolves promises before assigning them to x.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions