Full answer
Trace the value (or error) along the chain.
- Start: resolved with 1.
- then 1 prints 1 and returns 2.
- then 2 prints 2 and throws. The chain is now rejected.
- The next then has only a success handler, so it is skipped. Nothing is printed for 'skipped'.
- catch prints boom and returns 'recovered'. The chain is fulfilled again with that value.
- finally prints finally. It receives no argument. Its return value ('ignored') does not change the chain. If finally threw an error, or returned a rejected promise, the chain would reject.
- then 3 receives 'recovered' and prints it.
- The last two lines: .then(Promise.resolve('not a function')) passes a promise, not a function, as the handler. The spec says to ignore non-function handlers, so 'x' passes through to the end.
Output: then 1: 1, then 2: 2, catch: boom, finally, then 3: recovered, non-function then -> x
Other facts: .catch(fn) is .then(undefined, fn). The second argument of then handles errors from earlier in the chain but not from the first handler of the same then. A handler that returns a promise makes the chain wait for it.
javascript
Promise.resolve(1)
.then((v) => { console.log('then 1:', v); return v + 1; })
.then((v) => { console.log('then 2:', v); throw new Error('boom'); })
.then(() => console.log('skipped'))
.catch((e) => { console.log('catch:', e.message); return 'recovered'; })
.finally(() => { console.log('finally'); return 'ignored'; })
.then((v) => console.log('then 3:', v))
.then(() => Promise.resolve('x').then(Promise.resolve('not a function')))
.then((v) => console.log('non-function then ->', v));The output lines follow the value along the chain. Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, same on two runs): then 1: 1 then 2: 2 catch: boom finally then 3: recovered non-function then -> x
Key points
- A returned value flows to the next then.
- A thrown error skips success handlers until a catch.
- finally passes the value through and ignores its return value.
- Non-function arguments to then are ignored.
Common mistakes
- Expecting 'skipped' to print.
- Thinking the finally return value replaces the chain value.
- Passing a promise to then instead of a function.
- Forgetting to return inside then and passing undefined on.
Follow-ups
- What changes if finally throws?
- What is the difference between .then(a, b) and .then(a).catch(b)?
- What happens with an error thrown inside catch?
Tips
- Trace one value across the chain, step by step.
- State the finally rule explicitly.
- Mention the non-function rule.