Promises and Async/Await

📘 Node.js 👁 53 views 📅 Nov 05, 2025
⏱ Estimated reading time: 2 min

Promises and async/await provide modern, cleaner ways to handle asynchronous operations in JavaScript and Node.js, replacing deeply nested callbacks.


1. What Is a Promise?

A Promise is an object that represents the eventual result of an asynchronous operation.

A Promise can be in one of three states:

  • Pending – operation in progress

  • Fulfilled – operation completed successfully

  • Rejected – operation failed


2. Creating a Promise

const fetchData = new Promise((resolve, reject) => { const success = true; if (success) { resolve('Data received'); } else { reject('Error occurred'); } });

3. Consuming a Promise

fetchData .then(result => { console.log(result); }) .catch(error => { console.error(error); }) .finally(() => { console.log('Operation completed'); });

4. Promise Chaining

Promises can be chained to avoid callback nesting.

getUser() .then(user => getOrders(user)) .then(orders => processOrders(orders)) .catch(err => console.error(err));

5. Promise Utility Methods

MethodPurpose
Promise.all()Runs promises in parallel
Promise.race()Resolves first completed
Promise.any()Resolves first fulfilled
Promise.allSettled()Returns all results

6. Async / Await

async/await is syntactic sugar built on top of promises, making asynchronous code look synchronous.


6.1 Using async and await

async function fetchData() { try { const result = await fetchPromise(); console.log(result); } catch (error) { console.error(error); } }

✔ Easier to read
✔ Easier to debug


7. Error Handling with Async/Await

try { await riskyTask(); } catch (err) { console.log(err.message); }

8. Parallel Execution with Async/Await

const [a, b] = await Promise.all([task1(), task2()]);

9. Promises vs Async/Await

PromisesAsync/Await
Uses .then()Uses await
Chain-basedSequential-looking
Slightly verboseCleaner syntax

10. Common Mistakes

❌ Forgetting await
❌ Not handling errors
❌ Using await inside loops incorrectly


11. Best Practices

  • Always handle errors

  • Use Promise.all() for parallel tasks

  • Prefer async/await for readability

  • Avoid mixing callbacks with promises


12. Summary

  • Promises manage asynchronous results

  • Async/await simplifies promise handling

  • Improves code clarity and maintainability

  • Essential for modern Node.js development


🔒 Some advanced sections are available for Registered Members
Register Now

Share this Post


← Back to Tutorials

Popular Competitive Exam Quizzes