Learn how Promises manage asynchronous operation outcomes using resolve, reject, .then(), and .catch().
Learn how variables act as labeled containers for data using const, let, and var to manage lexical scope and reassignment safely in modern applications.
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().Promises encapsulate async state. Next, let's make Promises look synchronous with async/await!