Full answer
Synchronous part:
- main() starts. fails('not awaited') returns a rejected promise that nobody awaits. try/catch only catches errors from await, so caught 1 never prints. That promise stays unhandled.
- Then return fails('returned') returns the rejected promise without await. The function finishes before the rejection is handled, so the catch block does not run (caught 2 never prints). main's own promise follows the rejected promise and will be rejected.
- The two promise chains are set up, and sync end is printed first.
Microtasks, in rounds:
- Round 1: catch p1 (the first catch handler runs and throws p2), finally (the finally callback of the second chain runs).
- Round 2: the second catch in the first chain handles p2 and prints catch p2.
- Later rounds: main's promise needs a few extra rounds to adopt the rejected promise, so outer caught: returned is printed next, followed by then runs (the first chain recovers after catch). The finally chain also needs extra rounds to pass the original rejection on, so after finally: x comes after them.
After the microtask queue is empty, Node checks for rejections with no handler. The promise from fails('not awaited') is still unhandled, so the unhandledRejection listener prints its message last. Without the listener, Node 15 and later would crash the process with an error.
Lessons: always await or return and handle promises, use return await inside try when you want to catch the error there, and treat unhandled rejections as bugs. Exact order of the middle lines depends on the number of microtask hops, which is why you should not rely on it in real code.
process.on('unhandledRejection', (reason) => console.log('unhandledRejection:', reason.message));
async function fails(label) { throw new Error(label); }
async function main() {
try { fails('not awaited'); } catch (e) { console.log('caught 1'); }
try { return fails('returned'); } catch (e) { console.log('caught 2'); }
}
main().catch((e) => console.log('outer caught:', e.message));
Promise.reject(new Error('p1'))
.catch((e) => { console.log('catch', e.message); throw new Error('p2'); })
.catch((e) => console.log('catch', e.message))
.then(() => console.log('then runs'));
Promise.reject(new Error('x'))
.finally(() => console.log('finally'))
.catch((e) => console.log('after finally:', e.message));
console.log('sync end');The snippet is the puzzle itself. The output is the order of the log lines. The ordering of the middle lines follows the number of promise steps, and the output shown comes from one run on Node.js v24.22.1. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, run once): sync end catch p1 finally catch p2 outer caught: returned then runs after finally: x unhandledRejection: not awaited
Key points
- try/catch catches only awaited rejections.
- return without await skips the catch of the same function.
- A catch that throws passes a new error to the next catch.
- finally passes the original result or error on.
- Unhandled rejections are reported after microtasks drain, and crash Node by default.
Common mistakes
- Expecting the catch block to run for a promise that was not awaited.
- Using return promise inside try and expecting catch to work.
- Thinking finally can swallow the error.
- Ignoring unhandledRejection warnings.
- Relying on the exact order of unrelated promise chains.
Follow-ups
- What changes if main used return await fails('returned')?
- What does the default unhandled rejection mode do in Node 15 and later?
- How can you detect floating promises with a linter?
Tips
- Explain the two missed catches first.
- Say that the middle order depends on microtask hops.
- Mention return await.