Quick Overview
A promise settles once and keeps its result, so a `.then()` attached afterwards still fires. `.catch()` handles a rejection from anywhere earlier in the chain, which is why one at the end usually suffices.
A Promise is a built-in object representing the eventual completion or failure of an asynchronous operation.
Promises replace nested callback spaghetti with clean, chainable asynchronous pipelines that propagate errors safely down the chain.
pending: Initial state; operation is still in progress.fulfilled: Operation completed successfully (resolve(value)).rejected: Operation failed (reject(error)).const fetchUserData = new Promise((resolve, reject) => {
const success = true;
if (success) {
resolve({ id: 101, name: "Alex" });
} else {
reject("Network connection failed.");
}
});
fetchUserData
.then((user) => {
console.log("Loaded:", user.name);
return user.id;
})
.then((id) => console.log("User ID:", id))
.catch((err) => console.error("Error:", err));
Promise.all([p1, p2]): Fails fast if ANY promise rejects. Resolves when ALL pass.Promise.allSettled([p1, p2]): Waits for all promises to finish regardless of success or failure..catch() handler or use try...catch with async/await. Unhandled Promise rejections emit runtime warnings and can crash Node.js servers!.then() flattens the chain automatically. Do not nest .then() calls inside .then().Write it yourself. The tests below run against your code.
Promises encapsulate async state. Next, let's make Promises look synchronous with async/await!
Primitives copy by value and objects copy by reference, so `b = a` duplicates a number but shares an object. That single distinction explains most surprise mutations in a codebase.
`const` prevents reassignment, not mutation: a `const` object's properties can still be changed. `let` is scoped to its block, while `var` is function-scoped and leaks out of loops.
Only backticks interpolate: `${user}` reads the variable, while the identical text in quotes prints literally. Strings are immutable, so every method returns a new one rather than editing in place.