Understanding Node.js Promises in Asynchronous Operations

🚀 Node.js Interview Question #2: What are Promises in Node.js? A Promise is an object that represents the eventual result of an asynchronous operation. It helps handle async tasks like: - API calls - Database queries - File operations A Promise has 3 states: 1️⃣ Pending – operation not completed 2️⃣ Fulfilled – operation successful ("resolve") 3️⃣ Rejected – operation failed ("reject") 📌 Syntax Example const promise = new Promise((resolve, reject) => { let success = true; if (success) { resolve("Operation successful"); } else { reject("Operation failed"); } }); promise .then(result => console.log(result)) .catch(error => console.log(error)); 💡 Why Promises? They help avoid callback hell and make asynchronous code cleaner and easier to manage. #NodeJS #JavaScript #BackendDevelopment #TechInterview

To view or add a comment, sign in

Explore content categories