Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Asynchronous JavaScript  ›  Lesson

Promises

Promises 12 minA Promise is a claim ticket for a value that isn't ready yet: it starts pending, then settles exactly once, either fulfilled (with a value, handled by .then) or rejected (with a reason, handled by .catch), and .finally always runs afterwards regardless of which happened.

Promises

In short: a Promise is a claim ticket for a value that isn't ready yet. It starts pending, and settles exactly once — either fulfilled, with a value (.then handles this), or rejected, with a reason (.catch handles this). .finally runs afterwards no matter which happened, the same way finally always runs after try/catch. Once settled, a Promise's outcome is locked in forever; it can never change or settle a second time.

A pending claim ticket, checkStock("mug"), splitting into two paths: fulfilled, where .then(msg => ...) runs, or rejected, where .catch(err => ...) runs — with a note that .finally(() => ...) runs either way, cleanup, always

Waiting for stock to be checked

Wrapping an async operation in a Promise gives it a clean, single result to react to, instead of a callback:

javascript

checkStock returns a Promise immediately — pending. Once the simulated check finishes, it calls resolve with the stock message, which settles the Promise as fulfilled. await pauses here until that happens, then gives you the resolved value directly.

then, catch, and finally, all in one chain

The three handlers cover fulfilled, rejected, and "either way":

javascript

checkStock("sold-out-item") rejects, so .then is skipped entirely — only .catch runs, receiving the Error. .finally runs after either outcome, exactly like a finally block: the right place for cleanup that must happen whether the operation succeeded or failed.

Running several checks at once

Promise.all waits for every Promise in an array to fulfill, and gives you their results in the same order — a clean way to run independent async work in parallel instead of one after another:

javascript

All three checks run at the same time, not one after another — the total wait is roughly one check's worth of time, not three. If any one of them had rejected, Promise.all itself would reject immediately with that reason, skipping the rest.

Quick practice

quick drill

What are the three possible states of a Promise?

quick drill

If a Promise rejects, which handler runs?

quick drill

Why use Promise.all instead of awaiting three checkStock calls one after another with separate await lines?

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainSettling exactly once

Explain what "a Promise settles exactly once" means, using checkStock("mug") as an example, and why a Promise can never be both fulfilled and rejected, or fulfilled twice.