JavaScript Promise Resolution and Error Handling

🚀 Day 28 - 💡 JavaScript Tricky Question Explanation Promise.resolve(1)  .then(x => x + 1)  .then(x => { throw x; })  .catch(x => x + 1)  .then(x => console.log(x)); ✨ Output: 3 🧠 Explanation: 1️⃣ Promise.resolve(1) 👉 Starts resolved with value = 1 2️⃣ .then(x => x + 1) 👉 1 + 1 = 2 → passed to next then 3️⃣ .then(x => { throw x; }) 👉 throws 2 ❌ → Promise becomes rejected 👉 Skips remaining then and jumps to catch 4️⃣ .catch(x => x + 1) 👉 receives 2 → returns 3 👉 Promise becomes resolved again 5️⃣ .then(x => console.log(x)) 👉 logs final value → 3 📌 Key Points: - throw inside then = reject - catch handles error and can return new value - after catch, chain continues normally #JavaScript #WebDevelopment #Frontend #Coding #JSConcepts

To view or add a comment, sign in

Explore content categories