Promise.all() vs Promise.allSettled() JavaScript Interview Concept

🚀 Promise.all() vs Promise.allSettled() — A Must-Know JavaScript Interview Concept If you’re preparing for frontend interviews, this question shows up a lot: 👉 What’s the difference between Promise.all() and Promise.allSettled()? Let’s break it down in a simple, interview-ready way 👇 🔹 Promise.all() — “All or Nothing” Promise.all() runs multiple promises in parallel but: • It resolves only if ALL promises succeed • If even one fails → it fails immediately (fail-fast) 📌 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 ✔️ Use when: • You need all results to proceed • Example: Page rendering depends on multiple APIs 🔹 Promise.allSettled() — “Get Everything” Promise.allSettled() waits for all promises to finish, no matter success or failure. • Returns status for each promise • Never fails as a whole 📌 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" } ] ✔️ Use when: • APIs are independent • Example: Dashboard widgets, analytics panels 🎯 Interview-Ready Answer 👉 Promise.all() → fails immediately if any promise fails 👉 Promise.allSettled() → waits for all and returns individual results 💡 Follow-up Question 👉 “How do you get only successful results if some APIs fail?” const results = await Promise.allSettled(promises); const successResponses = results .filter(item => item.status === "fulfilled") .map(item => item.value); ⚡ Key Takeaway Understanding async behavior is not optional anymore. ✔ Helps debug real-world issues ✔ Improves API handling strategies ✔ Makes your interview answers stand out Small concepts like this create a big difference in senior-level interviews. #JavaScript #AsyncProgramming #FrontendDevelopment #WebDevelopment #ReactJS #InterviewPreparation #Promises #CodingInterview 👉 Follow Rahul R Jain for more real interview insights, React fundamentals, and practical frontend engineering content.

Good Insight about Promise Api 👍🏻

Like
Reply
See more comments

To view or add a comment, sign in

Explore content categories