Understanding JavaScript Promises

Day 11/100 of JavaScript 🚀 Today’s topic: Promises A Promise represents the result of an asynchronous operation It has 3 states: - Pending - Fulfilled - Rejected 🔹Creating a Promise const promise = new Promise((resolve, reject) => { let success = true; if (success) { resolve("Done"); } else { reject("Error"); } }); 🔹Consuming a Promise promise .then(result => console.log(result)) .catch(error => console.error(error)); 🔹Chaining fetch(url) .then(res => res.json()) .then(data => console.log(data)) .catch(err => console.error(err)); Promises help handle asynchronous operations in a structured way and avoid deeply nested callbacks. #Day11 #JavaScript #100DaysOfCode

To view or add a comment, sign in

Explore content categories