JavaScript Promises: Async Code Handling with Resolve and Reject

● Interview Question: What are Promises in JavaScript? - A Promise is an object that represents the eventual completion or failure of an asynchronous operation. - It helps handle async code without callback hell. ● Promise States A Promise can be in one of three states: - Pending – Initial state - Fulfilled – Operation completed successfully - Rejected – Operation failed ● Creating Promise const promise = new Promise((resolve, reject) => { let success = true; if (success) { resolve("Task completed"); } else { reject("Task failed"); } }); ● Consuming Promise promise .then(result => console.log(result)) .catch(error => console.log(error)) .finally(() => console.log("Done")); where :- .then() -> handles success .catch() -> handles errors .finally() -> always executes Promises make async code readable and manageable #JavaScript #Promises #AsyncJavaScript #InterviewPrep #WebDevelopment #Frontend #MERN #LearnInPublic #CodingJourney #30DaysOfJavaScript #Backend #BDRM #BackendDevWithRahulMaheshwari

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories