Full answer
A Promise represents the result of work that has not finished yet, such as reading a file or asking a server for data. It is in one of three states. Pending means the work is running. Fulfilled means it finished with a value. Rejected means it failed with a reason. Once a promise leaves pending, it is settled and cannot change.
The constructor takes an executor function with two arguments, resolve and reject. Calling either settles the promise. If the executor throws, the promise rejects.
then(onFulfilled, onRejected) registers callbacks and returns a new promise. The value returned from a callback becomes the value of the new promise. If the callback returns another promise, the chain waits for it. If the callback throws, the new promise rejects. A rejection skips every then without a rejection handler until a catch is found, and after a catch returns normally the chain is fulfilled again.
Callbacks never run synchronously. Even for an already settled promise, they go to the microtask queue and run after the current code finishes.
finally runs on both outcomes, receives no argument, and passes the original result along.
function checkSeat(seats) {
return new Promise((resolve, reject) => {
setTimeout(() => {
if (seats > 0) resolve(seats);
else reject(new Error('No seats left'));
}, 10);
});
}
console.log('start');
checkSeat(3)
.then((seats) => {
console.log('seats:', seats);
return seats - 1;
})
.then((left) => {
console.log('after booking:', left);
return checkSeat(0);
})
.then(() => console.log('skipped, the promise rejected'))
.catch((error) => {
console.log('caught:', error.message);
return 'recovered';
})
.then((value) => console.log('chain continues:', value))
.finally(() => console.log('cleanup done'));
Promise.resolve().then(() => console.log('microtask'));
console.log('end of script');Run with Node.js v24.22.1 as js-5.js; two runs gave identical output. Output: start end of script microtask seats: 3 after booking: 2 caught: No seats left chain continues: recovered cleanup done
Key points
- Three states: pending, fulfilled, rejected; settling happens once.
- then, catch, and finally return new promises, which enables chaining.
- A value returned in a handler resolves the next promise; a thrown error rejects it.
- Errors skip forward to the next catch; catch can recover the chain.
- Handlers run as microtasks, after the current synchronous code.
Common mistakes
- Nesting then inside then like callbacks instead of returning the inner promise.
- Forgetting to return the promise inside a then, so the next step runs too early with undefined.
- Leaving out a catch, which causes an unhandled rejection that ends a Node.js process.
- Assuming resolve stops the executor; code after resolve() still runs.
Follow-ups
- What is the difference between then(a, b) and then(a).catch(b)?
- What happens if a promise is resolved with another promise?
- When does an unhandled rejection event fire in Node.js and in a browser?
Tips
- Draw the three states and explain "settled once" before talking about syntax.
- Point out that handlers are asynchronous even when the promise is already resolved; this ties to the event loop question that usually follows.
- Show that you return values and promises inside then. It is the most common bug in review.