codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🟢

Node.js

16 / 63 topics
13Child Processes14Clusters15Async Programming16Promises17Async/Await18Error Handling
Tutorials/Node.js/Promises
🟢Node.js

Promises

Updated 2026-05-15
10 min read

Promises

Introduction

In the world of JavaScript, especially when dealing with I/O-bound tasks such as network requests or file system operations, asynchronous programming is a necessity. Promises are one of the fundamental concepts for handling these asynchronous operations in a clean and manageable way. This tutorial will guide you through understanding what promises are, how they work, and how to use them effectively.

Concept

A promise represents a value that may be available now, or in the future, or never. It is an object representing the eventual completion or failure of an asynchronous operation. Promises have three states:

  1. Pending: The initial state, neither fulfilled nor rejected.
  2. Fulfilled: The operation completed successfully.
  3. Rejected: The operation failed.

Promises provide a more structured and readable way to handle asynchronous code compared to traditional callback functions. They allow you to chain operations together and handle errors in a centralized manner.

Examples

Basic Usage

Let's start with a basic example of creating and using a promise:

JavaScript
1const myPromise = new Promise((resolve, reject) => {
2setTimeout(() => {
3 resolve('Success!');
4}, 1000);
5});
6
7myPromise.then((result) => {
8console.log(result); // Output: Success!
9}).catch((error) => {
10console.error(error);
11});

In this example, we create a promise that resolves after one second. The then method is used to handle the resolved value, and the catch method handles any errors.

Chaining Promises

Promises can be chained together using the then method. This allows you to perform multiple asynchronous operations in sequence:

JavaScript
1const fetchData = () => {
2return new Promise((resolve) => {
3 setTimeout(() => {
4 resolve('Data fetched');
5 }, 1000);
6});
7};
8
9const processData = (data) => {
10return new Promise((resolve) => {
11 setTimeout(() => {
12 resolve(`Processed ${data}`);
13 }, 500);
14});
15};
16
17fetchData()
18.then(processData)
19.then((result) => {
20 console.log(result); // Output: Processed Data fetched
21})
22.catch((error) => {
23 console.error(error);
24});

In this example, fetchData returns a promise that resolves with some data. The processData function takes this data and returns another promise that processes it. The then methods are chained to handle each step of the process.

Handling Errors

Errors in promises can be handled using the catch method or by providing a second argument to the then method:

JavaScript
1const fetchData = () => {
2return new Promise((resolve, reject) => {
3 setTimeout(() => {
4 reject('Failed to fetch data');
5 }, 1000);
6});
7};
8
9fetchData()
10.then((data) => {
11 console.log(data);
12})
13.catch((error) => {
14 console.error(error); // Output: Failed to fetch data
15});

In this example, the promise is rejected after one second. The catch method handles the error and logs it to the console.

Using Promise.all

When you have multiple promises that need to be resolved before proceeding, you can use Promise.all. This method takes an array of promises and returns a new promise that resolves when all of the input promises have resolved:

JavaScript
1const promise1 = new Promise((resolve) => {
2setTimeout(() => resolve('One'), 500);
3});
4
5const promise2 = new Promise((resolve) => {
6setTimeout(() => resolve('Two'), 1000);
7});
8
9Promise.all([promise1, promise2])
10.then((results) => {
11 console.log(results); // Output: ['One', 'Two']
12})
13.catch((error) => {
14 console.error(error);
15});

In this example, Promise.all waits for both promises to resolve and then logs the results.

Using Promise.race

If you want to handle the first promise that resolves or rejects, you can use Promise.race. This method takes an array of promises and returns a new promise that resolves or rejects as soon as one of the input promises does:

JavaScript
1const promise1 = new Promise((resolve) => {
2setTimeout(() => resolve('One'), 500);
3});
4
5const promise2 = new Promise((resolve, reject) => {
6setTimeout(() => reject('Two'), 1000);
7});
8
9Promise.race([promise1, promise2])
10.then((result) => {
11 console.log(result); // Output: One
12})
13.catch((error) => {
14 console.error(error);
15});

In this example, Promise.race resolves as soon as the first promise (promise1) resolves.

What's Next?

After mastering promises, you can explore more advanced topics such as async/await, which provides a cleaner syntax for working with asynchronous code. Understanding both promises and async/await will greatly enhance your ability to write robust and maintainable JavaScript applications.

By following this tutorial, you should have a solid understanding of how to use promises in Node.js to handle asynchronous operations effectively.


PreviousAsync ProgrammingNext Async/Await

Recommended Gear

Async ProgrammingAsync/Await