Learn how to create your own promises, handle errors gracefully, and build clear chains of async logic.
- How to create a Promise
- The difference between resolve and reject
- How to consume Promises
- How to chain Promises
- Error handling using
.catch() - Avoiding common pitfalls like broken chains
- When and how to recover from failures in the chain
const cart = ["shoes", "pants", "kurta"];
// Consumer part of promise
const promise = createOrder(cart); // returns a promise
promise.then(function (orderId) {
proceedToPayment(orderId);
});function createOrder(cart) {
return new Promise(function (resolve, reject) {
if (!validateCart(cart)) {
return reject(new Error("Cart is not Valid"));
}
const orderId = "12345"; // Simulated DB insert
resolve(orderId);
});
}console.log(promise); // Promise {<pending>}
// Will become fulfilled once the async work is donepromise
.then(function (orderId) {
proceedToPayment(orderId);
})
.catch(function (err) {
console.log(err.message); // "Cart is not Valid"
});createOrder(cart)
.then(function (orderId) {
return orderId;
})
.then(function (orderId) {
return proceedToPayment(orderId);
})
.then(function (paymentInfo) {
console.log(paymentInfo); // "Payment Successful"
})
.catch(function (err) {
console.log(err.message);
});createOrder(cart)
.then(function (orderId) {
return orderId;
})
.catch(function (err) {
console.log("Caught error:", err.message);
return null; // Continue chain
})
.then(function (orderId) {
if (orderId) return proceedToPayment(orderId);
else throw new Error("Skipping payment due to order failure");
})
.then(function (paymentInfo) {
console.log("Payment Done:", paymentInfo);
})
.catch(function (err) {
console.log("Final catch:", err.message);
});Q1. What is the difference between resolve and reject in a Promise?
A: resolve() is called when the async task completes successfully, reject() when it fails.
Q2. Why is .catch() important in a Promise chain?
A: It ensures you gracefully handle failures and don’t crash your app or cause memory leaks.
Q3. What happens if a .then() throws an error?
A: The next .catch() in the chain will catch it.
Q4. What is Promise Chaining?
A: Returning a promise in a .then() so that subsequent .then() blocks wait for it to resolve.
- You can create custom promises using the
Promiseconstructor. - Use
.then()to attach success callbacks and.catch()for error handling. - Return from each
.then()to maintain a proper chain. - Chains stop on rejection unless caught and continued manually.
- A promise can be in 3 states: Pending, Fulfilled, or Rejected.
- Promises help replace nested callbacks and avoid callback hell.