Promises
A promise is an object for a value that is not ready yet. It starts pending, then fulfills or rejects once, and you attach reactions with then, catch, and finally.
Detailed Explanation
Asynchronous work in JavaScript used to mean callbacks. Nested callbacks lost error handling and stack traces. A promise is a standard wrapper: one success value, one error, many observers.
You build one with new Promise((resolve, reject) => { ... }). The executor runs synchronously when you construct the promise. Call resolve(value) to fulfill, reject(err) to reject. After that, further resolve/reject calls are ignored. If the executor throws, the promise rejects.
resolve of another promise adopts its state (thenable assimilation). resolve({ then: (ok) => ok(1) }) also follows that thenable. That is how libraries interop. It is also how a malicious thenable can run code later; treat untrusted objects as data, not as thenables.
Reactions are microtasks. p.then(onFulfill, onReject) returns a new promise for whatever the callback returns. A thrown error rejects that new promise. catch(fn) is then(undefined, fn). finally(fn) runs on either settlement and passes the original value or reason through unless fn throws.
A promise is not cancellable in the language. AbortSignal is how fetch and newer APIs cancel the work; the promise still settles (usually rejects with AbortError).
unhandledrejection fires when a rejection has no catch by the end of the turn. Always attach a handler, even if it only logs. await in an async function is a handler; a missing await of a rejecting promise is a leak.
What it costs you: an extra allocation and a microtask hop per then. That is cheap compared to I/O. Creating a promise for work that is already synchronous (Promise.resolve(compute())) still defers the consumer. Do not wrap CPU work in a promise and expect the page to stay smooth; the executor is still synchronous.
Reach for promises when an API is async (I/O, user gesture, worker message). Reach for combinators when you wait for several. Reach for async/await when the control flow is sequential.
Code Examples
function wait(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
wait(10).then(() => console.log("done"));function loadUser(id) {
return new Promise((resolve, reject) => {
if (id <= 0) {
reject(new Error("bad id"));
return;
}
resolve({ id, email: "ada@example.com" });
});
}
loadUser(7)
.then((user) => user.email)
.then((email) => console.log(email)) // => ada@example.com
.catch((err) => console.error(err.message));
loadUser(-1).catch((err) => console.log(err.message)); // => bad idPromise.resolve(1)
.then((n) => n + 1)
.finally(() => console.log("settled"))
.then((n) => console.log(n)); // => settled then 2Common Gotchas
The executor is synchronous
`new Promise(() => { while (true) {} })` blocks the thread. Only `resolve` is deferred.
Unhandled rejections are process-level events
a forgotten `catch` can crash Node (`--unhandled-rejections=strict`) or log in the browser.
`then` always returns a new promise
chaining is not optional sugar; it is how errors propagate.
Resolving with a thenable adopts it
`resolve(p)` does not nest a promise inside a promise.