Chapter 2 · Promise Pizza Co.
You get a buzzer now. The pizza comes later.
At Promise Pizza Co. nobody waits at the counter. You get a buzzer the moment you order, and it goes off with either a pizza or bad news. That buzzer is a Promise.
The short answer
A Promise is a placeholder for a value that arrives later. It starts pending and settles once, as fulfilled or rejected. The executor runs immediately; .then, .catch and .finally handlers run later as microtasks, and each returns a new promise, which is what makes chaining and error propagation work.
Pending, fulfilled, and when each piece of code actually runs.
You place an order and new Promise(...) hands you a buzzer right away. It's pending: no pizza yet.
Byte: You place an order and new Promise(...) hands you a buzzer right away. It's pending: no pizza yet.
Swipe here or tap ▶ to step through1const order = new Promise((resolve, reject) => {2console.log('Oven on');3setTimeout(() => resolve('margherita'), 1000);4});56order.then((pizza) => console.log('Got', pizza));7console.log('Waiting...');
Nothing logged yet…
- Buzzer = a promise you hold while you wait
- Pizza box = the value a promise fulfilled with
- Burnt ball = the reason a promise rejected
- Sunken pedestal = a handler that got skipped
- Numbered pedestal = one handler in the chain (see the strip below the scene)
- Tip: use ← → to step, drag the scene to look around.
Your turn
Predict the output
Interviewers love these. Run the story in your head, pick an answer, and see why.
When does the executor (the function passed to new Promise) run?
1 / 61const p = new Promise((resolve) => {2console.log('A');3resolve('B');4console.log('C');5});6p.then(console.log);7console.log('D');
Before the interview
The cheat sheet
The whole chapter in a few lines, plus the follow-up questions people get caught on.
- 1A Promise is a placeholder for a future value: pending, then fulfilled or rejected. It settles once; later
resolve/rejectcalls are ignored. - 2The executor in
new Promise(fn)runs synchronously. Only the handlers (.then,.catch,.finally) run later, as microtasks. - 3
.then,.catchand.finallyeach return a new promise. That's what makes chaining work. - 4A handler's return value becomes the next value. Return a promise and the chain waits for it. Forget
returnand the next step getsundefined. - 5A rejection or thrown error skips
.thenhandlers until the nearest.catch. A.catchthat returns a value recovers the chain. - 6
.finallyreceives no argument and passes the previous result (or error) through unchanged. - 7
all: every promise fulfills → array of values, first rejection rejects.allSettled: never rejects, reports each.race: first to settle.any: first to fulfill,AggregateErrorif all fail. - 8
asyncfunctions always return a promise.awaitunwraps it, and a rejection becomes athrowyou cantry/catch.
Interview traps
›Is the code inside new Promise(...) async?
No. The executor runs immediately and synchronously. Only the handlers attached with .then/.catch run later.
›.then(onOk, onErr) vs .then(onOk).catch(onErr)?
The second also catches errors thrown inside onOk. With the two-argument form, onErr only sees rejections from before that .then.
›How do you run independent requests in parallel with await?
Start them all first, then await Promise.all([...]). Awaiting each one in turn inside a loop runs them one after another.
›What happens to a rejection nobody handles?
The browser fires an unhandledrejection event and logs 'Uncaught (in promise)'. In Node it can crash the process.
Prefer reading?
The whole chapter, written out
Every step of every story, plus the practice questions with their answers.
The stories
›1. Order upPending, fulfilled, and when each piece of code actually runs.
const order = new Promise((resolve, reject) => {
console.log('Oven on');
setTimeout(() => resolve('margherita'), 1000);
});
order.then((pizza) => console.log('Got', pizza));
console.log('Waiting...');- line 1You place an order and new Promise(...) hands you a buzzer right away. It's pending: no pizza yet.
- line 2The function you pass to new Promise (called the executor) runs immediately and synchronously, inside the constructor. Nothing async about this part.
- line 3It starts a 1 second timer. resolve is a button the kitchen will press later.
- line 6.then() doesn't run anything yet. It registers a handler: a spot in line to receive the pizza.
- line 7Synchronous code keeps going. Nobody stands around waiting for the pizza.
- line 3One second later the timer fires and resolve('margherita') is called. The buzzer goes off: fulfilled!
- line 6Once a promise finishes, fulfilled or rejected, we say it has settled. Settling queues the .then handler as a microtask (a short job that runs as soon as the current code finishes), and the value travels to it.
- line 6The handler runs and logs the pizza.
- A promise settles exactly once. Calling resolve or reject again does nothing: a buzzer can't un-buzz.
›2. Chain & catchHow values and errors travel down a .then chain.
orderPizza() // rejects with 'out of dough'
.then((pizza) => box(pizza))
.then((boxed) => deliver(boxed))
.catch((err) => {
console.log('Sorry:', err);
return 'garlic bread';
})
.then((food) => console.log('Eating', food))
.finally(() => console.log('Buzzer returned'));- line 1Every .then, .catch and .finally attaches a handler and returns a brand-new promise. So this chain is a line of five stations.
- line 1The kitchen calls reject('out of dough'). The buzzer flashes red: rejected.
- line 2A rejection skips .then handlers. Nobody boxes a pizza that doesn't exist.
- line 3Skipped again. The error keeps falling through the chain, looking for a handler.
- line 4.catch finally catches it.
- line 5The catch handler gets the error as its argument.
- line 6Returning a value from .catch recovers the chain. The next promise is fulfilled with 'garlic bread'.
- line 8Back on the happy path, so this .then runs.
- line 8It logs the food it received.
- line 9.finally runs no matter what. It gets no argument and passes the previous result straight through.
- line 9Done. Rejections skip to the nearest .catch, and a .catch can turn an error back into a value.
›3. Group ordersPromise.all vs allSettled vs race vs any, on the same three orders.
const pepperoni = bake('pepperoni', 300); // fulfills at 300ms
const burnt = bake('burnt', 100, true); // rejects at 100ms
const veggie = bake('veggie', 500); // fulfills at 500ms
const orders = [pepperoni, burnt, veggie];
Promise.all(orders);
Promise.allSettled(orders);
Promise.race(orders);
Promise.any(orders);- line 4Three orders, three buzzers, all pending. One of them is going to fail.
- line 6Four ways to wait for a group of promises. Each one returns its own promise.
- line 2100ms: the 'burnt' order rejects.
- line 6Promise.all gives up at the first rejection. One bad pizza fails the whole group order.
- line 8Promise.race settles the same way as whichever promise settles first, success or failure. 'burnt' got there first.
- line 9Promise.any ignores rejections. It's waiting for the first success.
- line 1300ms: pepperoni is ready.
- line 9Promise.any fulfills with the first success. If every order failed, it would reject with an AggregateError.
- line 3500ms: veggie arrives. Promise.all already failed, so it doesn't care.
- line 7Promise.allSettled waits for every order and never rejects. You get a report: {status, value} or {status, reason} for each.
- In short: all = everyone or bust. allSettled = full report. race = first to finish. any = first success.
Practice questions, answered
›When does the executor (the function passed to new Promise) run?
const p = new Promise((resolve) => {
console.log('A');
resolve('B');
console.log('C');
});
p.then(console.log);
console.log('D');Answer: A C D B
The executor runs synchronously (A, C). resolve doesn't stop it. The .then handler is a microtask, so it runs after the synchronous D.
›A promise is resolved and rejected. What happens?
new Promise((res, rej) => {
res('first');
rej('second');
res('third');
}).then(console.log, console.error);Answer: Logs 'first'
A promise settles once. The first call wins and later resolve or reject calls are silently ignored.
›Follow the value down the chain.
Promise.resolve(1)
.then((x) => x + 1)
.then((x) => { throw x * 10; })
.then((x) => console.log('then', x))
.catch((e) => console.log('catch', e));Answer: catch 20
1 becomes 2, then the throw rejects the chain with 20. The next .then is skipped and .catch receives 20.
›The forgotten return.
Promise.resolve('dough')
.then((d) => { d + ' + sauce'; })
.then((x) => console.log(x));Answer: undefined
The arrow function has a block body without return, so it returns undefined. The next .then receives undefined.
›race vs any: what logs, in order?
const fast = new Promise((_, rej) => setTimeout(() => rej('fail'), 100));
const slow = new Promise((res) => setTimeout(() => res('ok'), 200));
Promise.any([fast, slow]).then(console.log);
Promise.race([fast, slow]).catch((e) => console.log('race:', e));Answer: race: fail, then ok
At 100ms race settles with the first result, a rejection, so 'race: fail' logs. any skips the failure and fulfills at 200ms with 'ok'.
›What does eat() resolve to?
async function eat() {
try {
await Promise.reject('cold');
} catch (e) {
return 'reheated';
}
}
eat().then(console.log);Answer: reheated
await turns a rejection into a thrown error, so try/catch handles it. The async function returns 'reheated', which fulfills its promise.
Go deeper
Read and watch next
ByteTown gives you the picture. These are the sources to read when you want every detail.
Finished the story?
Stamp it on your map. Progress is saved in this browser. Next stop: Hoisting Hotel.
Coming soon · Practice Mode
Want drills and mock interviews for this?
Join the waitlist and get early access when it opens.