Promises

Pending fulfilled rejected

Every promise is pending, fulfilled, or rejected. Pending means the work is still running. Fulfilled means a value is ready, while rejected means an error stopped the work. See also JavaScript promises for the same states in plain script.

const fast: Promise<string> = Promise.resolve("ok"); const bad: Promise<string> = Promise.reject(new Error("no")); fast.then((value: string) => { console.log(value); }); bad.catch(() => { console.log("caught"); });

ok
caught

Construct one

Use the Promise constructor to wrap timer or callback logic. Call resolve with the value or reject with the error. The executor runs right away when the object is created.

function wait(ms: number): Promise<string> { return new Promise((resolve) => { setTimeout(() => { resolve("done"); }, ms); }); } wait(20).then((value: string) => { console.log(value); });

done

Then returns

Then always returns a fresh promise, so calls can chain. Return a value to fulfill the next link. Return another promise to pause the chain until that inner work ends. The wait helper is repeated here, so the block runs on its own.

function wait(ms: number): Promise<string> { return new Promise((resolve) => { setTimeout(() => { resolve("done"); }, ms); }); } wait(50) .then((msg) => msg.length) .then((len) => { console.log(len); });

4

Catch at the end

Put one catch at the end of the chain to collect errors from any step. The handler gets the rejection reason as unknown. Log it, recover with a default, or rethrow to keep failing loudly.

function wait(ms: number): Promise<string> { return new Promise((resolve) => { setTimeout(() => { resolve("done"); }, ms); }); } wait(50) .then(() => { throw new Error("late boom"); }) .catch((err: unknown) => { console.log((err as Error).message); });

late boom

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What are the three states of a promise?

    Answer

    Pending, fulfilled and rejected

    A promise is pending while the work runs, then settles as fulfilled with a value or rejected with a reason.

  2. What happens to the function passed to the Promise constructor?

    Answer

    It runs at once and calls resolve or reject when the work ends

    The executor runs immediately, which is why Promise.reject inside it is already rejected before a handler is attached.

  3. What does then return?

    Answer

    A new promise, so calls can chain

    Each then gives a fresh promise that settles after its callback, which is what makes a chain read from top to bottom.

  4. Why is one catch at the end of a chain enough?

    Answer

    An error skips the remaining then steps and reaches the nearest catch

    A rejection or a throw travels down the chain until a handler takes it, so one final catch sees an error from any step above it.

  5. What does this program print? const fast: Promise<string> = Promise.resolve("ok"); const bad: Promise<string> = Promise.reject(new Error("no")); fast.then((value: string) => { console.log(value); }); bad.catch(() => { console.log("caught"); });

    Answer

    ok / caught

    The fulfilled promise queues its callback first, so ok comes out before the catch of the rejected one.

  6. What does this program print? function wait(ms: number): Promise<string> { return new Promise((resolve) => { setTimeout(() => resolve("done"), ms); }); } wait(10) .then((msg) => msg.length) .then((len) => { console.log(len); });

    Answer

    4

    The first step receives the string done and returns its length, and the second step prints that number.

  7. What does this program print? Promise.reject(new Error("no")) .catch(() => { return "fallback"; }) .then((value) => { console.log(value); });

    Answer

    fallback

    A catch handler that returns a value recovers the chain, so the next step receives that value and the error is gone.

  8. What does this program print? Promise.resolve(2) .then((n) => { n * 2; }) .then((n) => { console.log(n); });

    Answer

    undefined

    The first callback returns nothing because the multiplication is not returned, so the next step receives undefined.

  9. What does the compiler say about this program? const p: Promise<string> = "ok"; console.log(p);

    Answer

    error TS2322: Type 'string' is not assignable to type 'Promise<string>'.

    A plain string is not a promise, even when the promise would resolve to a string, so the annotation is rejected.

  10. What does this program print? Promise.resolve(1) .then((n) => Promise.resolve(n + 1)) .then((n) => { console.log(n); });

    Answer

    2

    A callback that returns a promise pauses the chain until that promise settles, and then the value flows to the next step.

Article author: Arthur Isaev