Skip to the workspace
Pebellslearn by building
Asynchronous JavaScript  ›  Lesson

Promises

Promises 12 min

javascript / Promises

Explore each scene at your own pace.

01 · Promises

Promises

Promises

A promise represents an eventual outcome: pending, fulfilled with a value, or rejected with a reason. then returns a new promise based on its handler’s result.

Return the next result from a handler so the chain can follow it.

See the mechanism

Walk through the modelPromisesAuthored snapshots of the example; use Run below to test the code.
Statepromise has no final outcome yet
The eventual result is represented separately from its consumer.
1 of 3

Follow one example

javascript

The first then extracts a title and returns it. The next receives that returned value. A thrown error rejects the next promise, and catch can handle a rejection. Promise.resolve does not move synchronous heavy work to a background thread; the executor of new Promise runs synchronously too. Settled outcomes do not change to another state.

Predict, change and check

Predict: If the first handler uses braces but forgets return, what does the second handler receive?

Test your explanation: Make that mistake and observe undefined, then repair it. Throw an Error in the first handler and follow the rejection to catch.

The tasks below ask you to explain that result and apply the idea to different inputs.

Apply and distinguish

The next promise follows the handler result

then returns a new promise. Returning a value fulfills the next step with that value; throwing rejects it; returning another promise makes the chain follow its eventual outcome. Forgetting return inside a block handler often passes undefined to the next step.

The executor passed to new Promise runs synchronously. Creating a promise does not itself move a heavy loop onto another thread. Attach rejection handling intentionally, and return a chain so callers can await its actual completion. A settled promise does not later switch from fulfilled to rejected.

Check your understanding: State the input contract, identify the operation that changes the result, and give one boundary input that would distinguish the correct rule from the mistake described above.

Scene 1 of 2
Project context and further resources
Additional resources
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.