JavaScript Promise Static Methods: All, AllSettled, Race, Any

🚀 JavaScript Promise Static Methods — (Part: 2) 🔹 What are Promise Static Methods? These are built-in methods available on the Promise object , used to handle multiple promises efficiently. 🔹 1. Promise.all() — “All or Nothing” 👉 Runs multiple promises in parallel and waits for all to succeed Promise.all([p1, p2, p3]) .then(results => console.log(results)) .catch(error => console.log(error)); >> Returns all results in an array >> Fails immediately if any one promise fails Use Case: When all results are required 👉 Example: Fetching user data + posts + comments together 🔹 2. Promise.allSettled() — “I Want Everything” 👉 Waits for all promises (success + failure) Promise.allSettled([p1, p2, p3]) .then(results => console.log(results)); >> Returns status of each promise >> Never fails Use Case: When you want all results, even failed ones 👉 Example: Showing multiple API results with partial failures 🔹 3. Promise.race() — “Fastest Wins” 👉 Returns the first promise that completes (success or failure) Promise.race([p1, p2, p3]) .then(result => console.log(result)) .catch(err => console.log(err)); 👉 Example: API call vs timeout promise 🔹 4. Promise.any() — “First Success Wins” 👉 Returns the first successful promise Promise.any([p1, p2, p3]) .then(result => console.log(result)) .catch(err => console.log(err)); >> Ignores failed promises >> Fails only if all promises fail Use Case: Multiple APIs, take fastest successful response 🔹 Key Takeaways ✔️ These methods help manage multiple async operations ✔️ Improve performance with parallel execution ✔️ Avoid complex callback structures ✔️ Widely used in real-world applications #JavaScript #Promises #AsyncProgramming #WebDevelopment #Frontend #Coding #Developers #Tech #Learning

To view or add a comment, sign in

Explore content categories