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.
Read more
- Async/await (https://javascript.info/async-await)javascript.info
- await (MDN) (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await)developer.mozilla.org