Promises in depth · core
Chaining, error propagation, catch and finally
Every .then, .catch and .finally returns a new promise: returning a value fulfills it, throwing rejects it, returning a promise makes it follow that promise, and a rejection skips forward to the nearest rejection handler, after which the chain continues normally.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- then returns a new promise. The handler's return value fulfills it; a throw rejects it; a returned promise is adopted.
- A missing handler passes the value or the reason through to the next step.
- .catch(fn) is .then(undefined, fn). After a catch returns normally, the chain is fulfilled again (recovered).
- In then(onOk, onErr), onErr does NOT catch errors thrown by onOk in the same then. then(onOk).catch(onErr) does.
- finally(fn) gets no arguments and passes the original value or reason through, unless fn throws or returns a rejected promise, which replaces the outcome.
- Forgetting return inside then breaks the chain: the next step gets undefined, runs too early, and errors are not caught.
- Unhandled rejections fire the 'unhandledrejection' event in browsers and crash Node by default. End every chain with catch, or await it inside try.
The red pen
Common traps: mistake → fix
Mistake: A .then after .catch is skipped
Fix: it runs with the catch's return value.
Mistake: finally can change the value
Fix: only by throwing or rejecting.
Mistake: then(ok, err) catches errors thrown in ok
Fix: it doesn't.
Easy to hard
Questions interviewers ask, easy to hard
- What does .then return?
- How does an error travel through a chain?
- catch in the middle vs at the end: what is the difference?
- What does finally receive and return?
- then(a, b) vs then(a).catch(b); show the missing-return bug.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.