Synchronous exceptions thrown from complex expressions create abandoned promises. Solutions?

I just realised that Promise.all does not take a list of promises. It takes an iterable of promises. Which means it is possible already to write

try {
  const [a, b, c] = await Promise.all(function*() {
    yield new Promise((_, reject) => setTimeout(reject, 1, "first"));
    yield Promise.reject("second");
    throw new Error("third");
  }());
} catch(e) {
  console.error(e);
}

which does not cause an unhandled rejection. I have to admit it's totally ugly, but it seems to have exactly the semantics you asked for?

1 Like