Learn JavaScript
❯ async.promises

Promises in JavaScript

$concept

Promises represent eventual values with three states: pending, fulfilled (value), rejected (reason). new Promise((resolve, reject) => ...) wraps callback work; .then(onValue) chains transformations (returning new promises — thenables flatten automatically); .catch handles rejections; .finally runs regardless. Once settled, a promise never changes — caching and sharing are safe.

Promise construction is eager (executor runs immediately) while consumption chains lazily. Unhandled rejections crash modern Node and warn in browsers — every chain needs a terminal catch (or await in try/catch).

$how it works
1

Three states, one settlement

pending → fulfilled(value) or rejected(reason), exactly once — settled promises are immutable facts.

2

then chains forward

.then(v => ...) returns a new promise of the callback's result — sequential async reads top-to-bottom.

3

catch handles, finally cleans

.catch at chain end handles any prior rejection; .finally runs for cleanup on both paths.

4

Eager construction

The executor runs during new Promise — work starts immediately, .then merely observes the outcome.

$example
function slowDouble(n) {
return new Promise((resolve) => {
setTimeout(() => resolve(n * 2), 10);
});
}
slowDouble(21)
.then((v) => v + 1)
.then((v) => console.log(v));
Output
43
1slowDouble returns a promise settling to 42 after 10ms — work starts at construction.
2First .then adds 1 (new promise of 43); second logs it.
343 prints asynchronously — chained transformation of an eventual value.
$use cases
Wrapping callback APIs (timers, fs, XHR) in promises
Sequential async workflows reading top-to-bottom
Terminal .catch on every chain (or awaited try/catch)
Sharing settled promises as safe caches

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in JavaScript yourself — building beats watching.