tc39 / proposal-array-from-async

Draft specification for a proposed Array.fromAsync method in JavaScript.
http://tc39.es/proposal-array-from-async/
BSD 3-Clause "New" or "Revised" License
177 stars 13 forks source link

will it always close sync iterators? #39

Closed vadzim closed 9 months ago

vadzim commented 1 year ago

for await is known to not close sync iterables if rejected promise is emited. Like

function* It() {
  try {
    yield Promise.resolve("a")
    yield Promise.reject("b")
  } finally {
    console.log("finalized")
  }
}

// prints "a" and never prints "finalized"
for await (const x of It()) { console.log(x) }

// prints "a" and then prints "finalized"
for (const x of It()) { console.log(await x) }

which of theses behaviors will implement Array.fromAsync?

mgaudet commented 1 year ago

As implemented, we have the same semantics as for await (const x of It()) ..., and so the iterable is not closed.

bakkot commented 1 year ago

@mgaudet There's actually been consensus to change the behavior for for await over sync iterators so that it closes them for... over a year now, I guess; it's blocked on lack of tests. Since this proposal uses the same machinery it will be affected as well.