Typorithm · medium

Promises

promisesjavascript-corepromisethenstates
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Promises

0%

Async

A Promise represents a future value in one of three states: pending, fulfilled, or rejected. then handles success, catch handles errors, and Promise.all waits for several promises at once.

A Promise is an object representing a value that may not be ready yet. It starts pending and settles exactly once into fulfilled (resolve was called) or rejected (reject was called).

then registers a callback for the fulfilled value and returns a new promise, so calls chain: p.then(...).then(...).catch(...). catch handles a rejection anywhere earlier in the chain.

Promise.all([a, b]) takes an array of promises and fulfills with an array of their results once all settle successfully; it rejects immediately if any input rejects.

01

A promise is pending, fulfilled, or rejected.

02

then handles success and returns a new promise for chaining.

03

Promise.all waits for an array of promises.

const p = new Promise((resolve, reject) => {
  resolve(42);
});
p.then((v) => v * 2).catch((e) => console.error(e));
Promise.all([a, b]).then(([ra, rb]) => ra + rb);

Read the implementation

Connect each part of the code to the concept before you type.

1. Create a promise

A Promise wraps an async result; the executor gets resolve and reject callbacks.

new Promise((resolve, reject) => {

2. Fulfill it

Calling resolve moves the promise from pending to fulfilled with the value 42.

resolve(42);

3. Chain with then

then runs when the promise fulfills and returns a new promise for chaining.

p.then((v) => v * 2)

4. Handle rejection

catch runs if any earlier step rejects, handling the error in the chain.

.catch((e) => console.error(e))

5. Wait for all

Promise.all waits for every promise to fulfill, or rejects as soon as one does.

Promise.all([a, b])

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Promises: Lesson & Practice · Typorithm | ApplyDjinn