CodePark/ questions / Promises & async/await
START PRACTICINGPRACTICE
Frontend question · JavaScript › Promises & async/await

What does this log?

async function load() {
  throw new Error("boom");
}
async function main() {
  try {
    return load();
  } catch (e) {
    console.log("inner");
  }
}
main().catch(() => console.log("outer"));
outer

The answer is outer. load() does not throw synchronously. As an async function, it returns a rejected promise. return load() passes that promise out of the try without awaiting it. So the local catch never sees the rejection, and main() rejects. The outer .catch then logs outer.

The tempting option "inner" would be right only with
return await load(). Then the rejection would throw inside the try.