DEV Community

Romulo Gatto
Romulo Gatto

Posted on

Introduction to Promises in Node.js

Introduction to Promises in Node.js

Promises are an essential part of asynchronous programming in Node.js. They provide a way to handle the results and errors of asynchronous operations more efficiently and elegantly. If you're new to promises or curious about how they work, this guide will give you a solid introduction to using promises in your Node.js applications.

What is a Promise?

In simple terms, a promise is an object that represents the eventual completion (or failure) of an asynchronous operation. It serves as a placeholder for the result of the operation that may not have been completed yet.

A promise can be in one of three states:

  1. Pending: The initial state before the promise settles.
  2. Fulfilled: The state when the promised action is completed successfully.
  3. Rejected: The state when the promised action encounters an error.

Promises can be chained together, allowing you to perform multiple asynchronous operations sequentially or concurrently with ease.

Creating Promises

Creating promises in Node.js is straightforward. Here's how you can create a basic promise:

const myPromise = new Promise((resolve, reject) => {
  // Asynchronous logic here
});
Enter fullscreen mode Exit fullscreen mode

The Promise constructor takes a function as its argument, commonly referred to as the executor. This executor function receives two parameters: resolve and reject.

Inside this executor function, you write your asynchronous code and eventually call either resolve with the desired result or reject with an error if something goes wrong.

Let's see an example using setTimeout:

const wait = (milliseconds) => {
  return new Promise((resolve) => {
    setTimeout(resolve, milliseconds);
  });
};

wait(2000)
  .then(() => console.log('Finished waiting!'))
  .catch((error) => console.error(error));
Enter fullscreen mode Exit fullscreen mode

In this example, we define a utility function wait that creates a promise using the Promise constructor. The resolve method is called after the specified number of milliseconds, simulating a delay. We can then chain .then() to handle the resolved promise and .catch() to catch any errors.

Chaining Promises

One of the powerful features of promises is their ability to be chained together, allowing you to perform sequential operations. This technique is known as promise chaining.

Here's an example that demonstrates how to chain promises:

const getUser = () => {
  return new Promise((resolve) => {
    resolve({ id: 1, name: 'John Doe' });
  });
};

const getAddress = (user) => {
  return new Promise((resolve) => {
    // Simulating an API call with setTimeout
    setTimeout(() => resolve(`123 Main St, ${user.name}`), 2000);
  });
};

getUser()
  .then(getAddress)
  .then((address) => console.log(address))
  .catch((error) => console.error(error));
Enter fullscreen mode Exit fullscreen mode

In this example, we define two functions: getUser and getAddress. Each function returns a promise that resolves with different values. By chaining these promises using .then(), we pass the result from one promise to another until reaching the final desired value.

Handling Errors

Promises offer exceptional error handling capabilities through their built-in .catch() method. It allows you to handle any errors occurring during promise execution gracefully.

Consider this example:

const fetchUser = () => {
  return new Promise((resolve, reject) => {
    // Simulating an API call with setTimeout
    setTimeout(reject('User not found'), 2000);
  });
};

fetchUser()
  .then(() =>
    console.log('User fetched successfully!')
  )
  .catch((error) =>
    console.error(`Error fetching user: ${error}`)
  );
Enter fullscreen mode Exit fullscreen mode

In this case, the promise returned from fetchUser is rejected, and we can catch the error with the .catch() method. This allows us to gracefully handle errors and provide relevant feedback or take appropriate actions.

Conclusion

Promises are a powerful tool for managing asynchronous operations in Node.js. They simplify complex async code by providing a structured way to handle success and failure scenarios. By understanding the basics of promises and how to chain them together, you'll have a solid foundation for writing efficient and robust Node.js applications.

So go ahead, dive into promises in Node.js, and experience cleaner asynchronous programming!

Top comments (0)