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.
-
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.
-
What happens to the function passed to the
Promiseconstructor?Answer
It runs at once and calls
resolveorrejectwhen the work endsThe executor runs immediately, which is why
Promise.rejectinside it is already rejected before a handler is attached. -
What does
thenreturn?Answer
A new promise, so calls can chain
Each
thengives a fresh promise that settles after its callback, which is what makes a chain read from top to bottom. -
Why is one
catchat the end of a chain enough?Answer
An error skips the remaining
thensteps and reaches the nearestcatchA rejection or a throw travels down the chain until a handler takes it, so one final
catchsees an error from any step above it. -
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.
-
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.
-
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.
-
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.
-
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.
-
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