Introduction
A JavaScript Promise is a special object that makes handling asynchronous tasks much easier. Instead of blocking the program while waiting for something like an API response, file load, or a timer, Promises act as placeholders for values that will become available later. Once the operation completes, the Promise either provides the result or an error.
Think of a Promise as a deal: it either fulfills its promise (success) or fails to do so (failure).
States of a Promise
A Promise can be in one of three states:
Pending – The task is still in progress and has not yet finished.
Fulfilled – The task completed successfully, and a result is available.
Rejected – The task failed, and an error is returned.
Once a Promise is either fulfilled or rejected, it is considered settled, and its result does not change.
Example: Checking if a Number is Even
let checkEven = new Promise((resolve, reject) => {
let number = 4;
if (number % 2 === 0) resolve("The number is even!");
else reject("The number is odd!");
});
checkEven
.then((message) => console.log(message)) // Runs if resolved
.catch((error) => console.error(error)); // Runs if rejected
📌 Note: resolve and reject are not keywords; you can name them anything.
General Syntax of a Promise
let promise = new Promise((resolve, reject) => {
// Perform some async operation
if (operationSuccessful) {
resolve("Task successful");
} else {
reject("Task failed");
}
});
resolve(value): Marks the promise as successful and passes the result.
reject(error): Marks the promise as failed with an error message.
Advanced Promise Methods and Patterns
1. Promise.all()
Runs multiple promises in parallel and waits until all succeed. If any fail, it rejects immediately.
Promise.all([
Promise.resolve("Task 1 done"),
Promise.resolve("Task 2 done"),
Promise.reject("Task 3 failed")
])
.then((results) => console.log(results))
.catch((error) => console.error(error));
Output:
Task 3 failed
2. Promise.allSettled()
Runs all promises and returns results for both fulfilled and rejected ones.
Promise.allSettled([
Promise.resolve("Task 1 done"),
Promise.reject("Task 2 failed"),
Promise.resolve("Task 3 done")
])
.then((results) => console.log(results));
Output:
[
{ status: 'fulfilled', value: 'Task 1 done' },
{ status: 'rejected', reason: 'Task 2 failed' },
{ status: 'fulfilled', value: 'Task 3 done' }
]
3. Promise.race()
Returns the result of the first promise that settles (success or failure).
Promise.race([
new Promise((resolve) => setTimeout(() => resolve("Task 1 finished"), 1000)),
new Promise((resolve) => setTimeout(() => resolve("Task 2 finished"), 500))
])
.then((result) => console.log(result));
Output:
Task 2 finished
4. Promise.any()
Resolves as soon as the first promise is fulfilled. Rejects only if all fail.
Promise.any([
Promise.reject("Task 1 failed"),
Promise.resolve("Task 2 done"),
Promise.resolve("Task 3 done")
])
.then((result) => console.log(result))
.catch((error) => console.error(error));
Output:
Task 2 done
5. Promise.resolve()
Creates a promise that resolves immediately with a given value.
Promise.resolve("Immediate success")
.then((value) => console.log(value));
Output:
Immediate success
6. Promise.reject()
Creates a promise that rejects immediately.
Join the conversation! Your thoughts help the community grow.