A promise is an object that represents the future result of an asynchronous operation: first it is in the pending state, then it “resolves” (fulfilled/resolved) or “rejects” (rejected). The .then method receives the value on success, .catch — the error on rejection.
- Handle the result when an async task (network, timer) finishes without blocking the code.
- Build transformation chains where the result of one .then is passed to the next.
Promise.resolve(5).then(value => {
console.log(value)
})Output:
5Promise.resolve(5) immediately creates a resolved promise; .then receives the value 5 and logs it.
Promise.resolve(2)
.then(x => x * 3)
.then(y => console.log(y))Output:
6The value 6 returned from the first .then goes into the second .then and is logged.
const p = new Promise(resolve => {
setTimeout(() => resolve("ready"), 10)
})
p.then(v => console.log(v))Output:
readyresolve is called inside the timer callback — the promise resolves when the setTimeout fires.
Key rules:
- ✓A promise changes state: pending → fulfilled or rejected, and only once.
- ✓.then — on success, .catch — on error; chains pass the result by value.
Common mistakes:
- !A .then callback runs asynchronously, after the current synchronous code.
- !If .then does not return a promise, the chain “breaks” — the result is lost for the next link.
An async function always returns a promise, and await “unwraps” the promise value inside it, pausing execution until it is done. Errors are caught with the usual try/catch.
- Write asynchronous code linearly, without nested .then.
- Handle rejections calmly via try/catch instead of long .catch chains.
async function f() {
return 7
}
console.log(f())Output:
Promise { 7 }Despite return 7, an async function returns a promise, not the number itself.
async function f() {
const x = await Promise.resolve(10)
return x + 1
}
f().then(v => console.log(v))Output:
11await “unfreezes” the promise value (10), we add 1 → 11.
async function f() {
try {
await Promise.reject("x")
} catch (e) {
console.log("caught: " + e)
}
}
f()Output:
caught: xawait on a rejected promise throws an exception that the usual catch handles.
Key rules:
- ✓An async function wraps the result in a promise even if it returns a simple value.
- ✓await works only inside async and returns the resolved value of a promise.
- ✓try/catch — the standard way to handle errors in async/await.
Common mistakes:
- !await outside an async function — SyntaxError.
- !Forgetting try/catch on await — the error becomes an “unhandled promise rejection”.
Promise.all waits for all promises and returns an array; on the first rejection it rejects at once. Promise.allSettled always waits for all and returns results in any state. Promise callbacks (microtasks) run before setTimeout (macrotasks).
- Run several operations in parallel and wait for all of them.
- Know the outcome of each operation even if some finished with an error.
Promise.all([
Promise.resolve(1),
Promise.resolve(2),
]).then(r => console.log(r))Output:
[ 1, 2 ]Promise.all returns an array of values in the same order as the passed promises.
Promise.allSettled([
Promise.resolve(1),
Promise.reject("x"),
]).then(r => console.log(r.length))Output:
2allSettled waits for all regardless of the outcome — the array has both results, length 2.
Promise.resolve().then(() => console.log("A"))
setTimeout(() => console.log("B"), 0)Output:
A
BA promise callback is a microtask and runs before the macrotask setTimeout, even with a 0 delay.
Key rules:
- ✓Promise.all rejects on the first rejection; allSettled — never.
- ✓Promise.all returns an array in the original order of the promises.
- ✓Microtasks (Promise) run before the next macrotask (setTimeout).
Common mistakes:
- !Promise.all “instantly” rejects if at least one promise is rejected — other results are lost.
- !Confusion all vs allSettled: all fails on error, allSettled collects every outcome.