A Promise is a JavaScript object that represents the eventual completion (or failure) of an asynchronous operation and its resulting value. Promises help you write cleaner, more readable asynchronous code and avoid deeply nested callbacks.
.then() for success and .catch() for errors..then() and build step-by-step workflows.Promise.all() let you work with multiple Promises together.You create a Promise using the new Promise() constructor. It takes an executor function with two parameters: resolve (for success) and reject (for failure).
resolve(value) → moves the Promise from pending to fulfilled.reject(reason) → moves the Promise from pending to rejected..then(onFulfilled) runs when the Promise is fulfilled..catch(onRejected) runs when the Promise is rejected..then() returns a new Promise, which enables chaining.This example shows how to create a Promise that may resolve or reject based on a condition.
let promise = new Promise(function(resolve, reject) {
// Do something...
let success = true;
if (success) {
resolve("Operation was successful!");
} else {
reject("There was an error.");
}
});
Use .then() to handle success and .catch() to handle errors.
promise.then(function(result) {
console.log(result);
}).catch(function(error) {
console.error(error);
});
A Promise that always resolves successfully.
let promise = new Promise(function(resolve, reject) {
resolve("Success!");
});
promise.then(function(message) {
console.log("Message:", message);
});
A Promise that is rejected and handled with .catch().
let promise = new Promise(function(resolve, reject) {
reject("Oops! Something went wrong.");
});
promise
.then(result => console.log(result))
.catch(error => console.error("Error:", error));
This example simulates a delayed operation using setTimeout().
let loading = new Promise(function(resolve, reject) {
setTimeout(() => {
resolve("Data loaded!");
}, 2000);
});
loading.then(result => console.log(result));
Each .then() can transform the value and pass it to the next .then().
new Promise((resolve, reject) => {
setTimeout(() => resolve(5), 1000);
})
.then(result => {
console.log(result); // 5
return result * 2;
})
.then(result => {
console.log(result); // 10
return result * 2;
});
Promise.all() runs multiple Promises in parallel and waits for all to finish.
let p1 = Promise.resolve(1);
let p2 = Promise.resolve(2);
let p3 = Promise.resolve(3);
Promise.all([p1, p2, p3]).then(values => {
console.log(values); // [1, 2, 3]
});
Message: Success! to the console..then() and logs Error: Oops! Something went wrong. from .catch().Data loaded!.5 first, then 10, showing how values flow through the chain.[1, 2, 3] when all Promises are fulfilled. If any one rejects, the whole Promise.all() rejects..catch() so failures don’t crash silently.Promise.all() when you need to run independent tasks in parallel and wait for all of them.Promise.resolve() and Promise.reject()..then() when you want proper chaining.setTimeout(), wrap them in a Promise if you want to chain them cleanly..then() calls to process a numeric result step-by-step (e.g., double, then add 10).Promise.all() with a few timeout-based Promises (1s, 2s, 3s) and log the array of results..then()/.catch() to handle it.