ByteTown
← ByteTown map

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 through
🎯 0/2
pending1
fulfilled0
rejected0
story-1.js
1const order = new Promise((resolve, reject) => {
2 console.log('Oven on');
3 setTimeout(() => resolve('margherita'), 1000);
4});
5
6order.then((pizza) => console.log('Got', pizza));
7console.log('Waiting...');
Console0 lines

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 / 6
1const p = new Promise((resolve) => {
2 console.log('A');
3 resolve('B');
4 console.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/reject calls are ignored.
  • 2The executor in new Promise(fn) runs synchronously. Only the handlers (.then, .catch, .finally) run later, as microtasks.
  • 3.then, .catch and .finally each 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 return and the next step gets undefined.
  • 5A rejection or thrown error skips .then handlers until the nearest .catch. A .catch that returns a value recovers the chain.
  • 6.finally receives no argument and passes the previous result (or error) through unchanged.
  • 7all: every promise fulfills → array of values, first rejection rejects. allSettled: never rejects, reports each. race: first to settle. any: first to fulfill, AggregateError if all fail.
  • 8async functions always return a promise. await unwraps it, and a rejection becomes a throw you can try/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...');
  1. line 1You place an order and new Promise(...) hands you a buzzer right away. It's pending: no pizza yet.
  2. line 2The function you pass to new Promise (called the executor) runs immediately and synchronously, inside the constructor. Nothing async about this part.
  3. line 3It starts a 1 second timer. resolve is a button the kitchen will press later.
  4. line 6.then() doesn't run anything yet. It registers a handler: a spot in line to receive the pizza.
  5. line 7Synchronous code keeps going. Nobody stands around waiting for the pizza.
  6. line 3One second later the timer fires and resolve('margherita') is called. The buzzer goes off: fulfilled!
  7. 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.
  8. line 6The handler runs and logs the pizza.
  9. 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'));
  1. line 1Every .then, .catch and .finally attaches a handler and returns a brand-new promise. So this chain is a line of five stations.
  2. line 1The kitchen calls reject('out of dough'). The buzzer flashes red: rejected.
  3. line 2A rejection skips .then handlers. Nobody boxes a pizza that doesn't exist.
  4. line 3Skipped again. The error keeps falling through the chain, looking for a handler.
  5. line 4.catch finally catches it.
  6. line 5The catch handler gets the error as its argument.
  7. line 6Returning a value from .catch recovers the chain. The next promise is fulfilled with 'garlic bread'.
  8. line 8Back on the happy path, so this .then runs.
  9. line 8It logs the food it received.
  10. line 9.finally runs no matter what. It gets no argument and passes the previous result straight through.
  11. 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);
  1. line 4Three orders, three buzzers, all pending. One of them is going to fail.
  2. line 6Four ways to wait for a group of promises. Each one returns its own promise.
  3. line 2100ms: the 'burnt' order rejects.
  4. line 6Promise.all gives up at the first rejection. One bad pizza fails the whole group order.
  5. line 8Promise.race settles the same way as whichever promise settles first, success or failure. 'burnt' got there first.
  6. line 9Promise.any ignores rejections. It's waiting for the first success.
  7. line 1300ms: pepperoni is ready.
  8. line 9Promise.any fulfills with the first success. If every order failed, it would reject with an AggregateError.
  9. line 3500ms: veggie arrives. Promise.all already failed, so it doesn't care.
  10. line 7Promise.allSettled waits for every order and never rejects. You get a report: {status, value} or {status, reason} for each.
  11. 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.

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.

No spam. One email when it launches, and you can unsubscribe anytime.