Promise.all() vs Promise.allSettled() in JavaScript

💡 JavaScript Interview Question: Promise.all() vs Promise.allSettled() While preparing for frontend interviews, one question that often comes up is: 👉 What is the difference between Promise.all() and Promise.allSettled()? Here is a simple way to explain it in interviews. 🔹 Promise.all() Promise.all() takes multiple promises and runs them in parallel. It resolves only when all promises are successful. If any one promise fails, the entire Promise.all() fails immediately. 📌 Example const p1 = Promise.resolve("API 1 success"); const p2 = Promise.resolve("API 2 success"); const p3 = Promise.reject("API 3 failed"); Promise.all([p1, p2, p3]) .then(res => console.log(res)) .catch(err => console.log("Error:", err)); Output: Error: API 3 failed ✔️ When to use: When all results are required to proceed. Example: Loading multiple dependencies before rendering a page. 🔹 Promise.allSettled() Promise.allSettled() waits for all promises to complete, regardless of success or failure. It returns the status of each promise (fulfilled or rejected). 📌 Example Promise.allSettled([p1, p2, p3]).then(res => console.log(res)); Output: [ { status: "fulfilled", value: "API 1 success" }, { status: "fulfilled", value: "API 2 success" }, { status: "rejected", reason: "API 3 failed" } ] ✔️ When to use: When you want results from all APIs even if some fail. Example: Dashboard widgets where each API is independent. 🎯 How I answer this in interviews Promise.all() → fails fast if any promise fails. Promise.allSettled() → waits for all promises and returns each result. 💬 Follow-up interview question "What if you want successful responses even when some APIs fail?" Answer: Use Promise.allSettled() and filter fulfilled results. const results = await Promise.allSettled(promises); const success = results .filter(r => r.status === "fulfilled") .map(r => r.value); Understanding these small JavaScript concepts deeply can make a big difference in frontend interviews. #javascript #reactjs #frontenddeveloper #webdevelopment #interviewprep #mernstack

To view or add a comment, sign in

Explore content categories