Promises in JavaScript
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).
Three states, one settlement
pending → fulfilled(value) or rejected(reason), exactly once — settled promises are immutable facts.
then chains forward
.then(v => ...) returns a new promise of the callback's result — sequential async reads top-to-bottom.
catch handles, finally cleans
.catch at chain end handles any prior rejection; .finally runs for cleanup on both paths.
Eager construction
The executor runs during new Promise — work starts immediately, .then merely observes the outcome.
function slowDouble(n) { return new Promise((resolve) => { setTimeout(() => resolve(n * 2), 10); });}slowDouble(21) .then((v) => v + 1) .then((v) => console.log(v));Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in JavaScript yourself — building beats watching.