This page provides detailed explanations and real examples of all major Promise handling methods in JavaScript. Includes abbr tags for new terms.
then() - Handling SuccessDefinition: The then() method is used to define what to do when a Promise is fulfilled (i.e., successful).
When to use: When you want to handle the result after a successful async task.
const promise = Promise.resolve("Data received");
promise.then(result => {
console.log("Success:", result);
});
catch() - Handling ErrorsDefinition: The catch() method handles rejected Promises (errors).
When to use: When you want to deal with failures like network errors or invalid operations.
const promise = Promise.reject("Something went wrong");
promise
.then(data => console.log(data))
.catch(error => console.error("Error caught:", error));
finally() - Always RunsDefinition: finally() runs after the promise settles, no matter if it was resolved or rejected.
Use case: Cleanup, hiding loaders, resetting states.
fetch("https://jsonplaceholder.typicode.com/posts/1")
.then(res => res.json())
.then(data => console.log(data))
.catch(err => console.error(err))
.finally(() => console.log("✅ Request completed"));
Promise.all() - Wait for AllDefinition: Promise.all() waits for all promises to succeed or fails if any one fails.
When to use: When you need all operations to complete before proceeding (e.g., multiple API calls).
const p1 = Promise.resolve("🟢 Task 1");
const p2 = Promise.resolve("🟢 Task 2");
Promise.all([p1, p2])
.then(results => console.log("All done:", results))
.catch(err => console.error("At least one failed:", err));
Promise.race() - First to SettleDefinition: Promise.race() returns the first settled promise (fulfilled or rejected).
Use case: Timeout handling or picking the fastest response.
const fast = new Promise(res => setTimeout(() => res("Fast!"), 500));
const slow = new Promise(res => setTimeout(() => res("Slow"), 2000));
Promise.race([fast, slow]).then(result => console.log("First done:", result));
Promise.allSettled() - Get All ResultsDefinition: This method returns results of all promises whether they are resolved or rejected.
Use case: When you want to know the outcome of every promise without short-circuiting.
const p1 = Promise.resolve("OK");
const p2 = Promise.reject("Failed");
Promise.allSettled([p1, p2]).then(results => console.log(results));
Promise.any() - First SuccessDefinition: Returns the first fulfilled promise. Ignores any rejections.
Use case: Get the first successful result, even if others fail.
const fail1 = Promise.reject("Fail 1");
const fail2 = Promise.reject("Fail 2");
const pass = Promise.resolve("Success!");
Promise.any([fail1, fail2, pass])
.then(result => console.log("First fulfilled:", result))
.catch(err => console.error("All failed:", err));
| Method | Purpose | Use Case |
|---|---|---|
then() |
Handle success | On fulfilled Promise |
catch() |
Handle errors | On rejected Promise |
finally() |
Always runs | Cleanup after resolve/reject |
Promise.all() |
Wait for all | All must succeed |
Promise.race() |
First to settle | Fastest wins |
Promise.allSettled() |
All outcomes | Get all results |
Promise.any() |
First success | One must succeed |