"Understanding Promise Methods in JavaScript"

Promises help us handle async tasks like fetching data without blocking code execution. Here are 4 common Promise methods with simple examples 👇 --- 💡 1️⃣ Promise.all() Waits for all promises to finish — fails if any one fails. Promise.all([ Promise.resolve("A"), Promise.resolve("B") ]).then(result => console.log(result)); ✅ Output: ["A", "B"] --- 💡 2️⃣ Promise.any() Returns the first successful promise. Promise.any([ Promise.reject("Error"), Promise.resolve("Success") ]).then(result => console.log(result)); ✅ Output: "Success" --- 💡 3️⃣ Promise.race() Returns the first promise that finishes (success or fail). Promise.race([ Promise.resolve("Fast"), Promise.reject("Error") ]).then(result => console.log(result)); ✅ Output: "Fast" --- 💡 4️⃣ Promise.allSettled() Waits for all promises — returns both success and failure results. Promise.allSettled([ Promise.resolve("Done"), Promise.reject("Failed") ]).then(result => console.log(result)); ✅ Output: [ { status: "fulfilled", value: "Done" }, { status: "rejected", reason: "Failed" } ] --- ✨ Summary: 🟢 all() → Waits for all success 🟢 any() → First success 🟢 race() → First finished 🟢 allSettled() → Get all results #JavaScript #Promises #WebDevelopment #Async #Coding

  • text

To view or add a comment, sign in

Explore content categories