Back to questions
JuniorTheoryCommon
What does the program print (lines separated by spaces)?
async function load() {
throw new Error("bad");
}
try {
load().catch(() => console.log(1));
console.log(2);
} catch (e) {
console.log(3);
}2 1
- Expecting
3: athrowinside anasyncfunction does not throw at the call; it rejects the returned promise, so only.catchsees it. - Expecting
1first: the.catchcallback waits in the microtask queue until the current code, includingconsole.log(2), has finished.
- How do you change the program so that the
try…catchblock catches the error? - What happens if you remove
.catch(() => console.log(1))?