Node.js Tutorial: Understanding Promise.all and Promise.race 🎯

beginner
10 min

Node.js Tutorial: Understanding Promise.all and Promise.race 🎯

Welcome to this comprehensive guide on Promise.all and Promise.race in Node.js! In this lesson, we'll delve into these powerful concepts, explaining why they're essential for managing asynchronous operations in your Node.js projects. Let's dive in!

What are Promises in Node.js? 📝

Promises in Node.js are objects that represent the eventual completion or failure of an asynchronous operation. They provide a way to handle the result (value) or reason for rejection (error) of an asynchronous operation.

javascript
let promise = new Promise((resolve, reject) => { // asynchronous operation goes here // if successful, resolve(result) // if failed, reject(error) });

Introducing Promise.all and Promise.race 💡

Promise.all and Promise.race are two static methods in Node.js that help manage multiple promises.

Promise.all 🎯

Promise.all takes an array of promises as an argument and returns a new promise that resolves when all of the input promises have resolved, or rejects with the reason of the first promise that rejects.

javascript
let promise1 = new Promise((resolve, reject) => { // promise 1 }); let promise2 = new Promise((resolve, reject) => { // promise 2 }); let promises = [promise1, promise2]; let allPromises = Promise.all(promises) .then(results => { // handle results }) .catch(error => { // handle error });

Promise.race 🎯

Promise.race takes an array of promises as an argument and returns a new promise that resolves or rejects as soon as one of the promises in the array resolves or rejects.

javascript
let promise1 = new Promise((resolve, reject) => { // promise 1 }); let promise2 = new Promise((resolve, reject) => { // promise 2 }); let promises = [promise1, promise2]; let racePromises = Promise.race(promises) .then(result => { // handle result }) .catch(error => { // handle error });

Practical Examples 📝

Let's consider a real-world example where we have multiple API requests that fetch data from different sources.

javascript
let promise1 = fetch('https://api.example1.com') .then(response => response.json()) .then(data => { // process data }) .catch(error => { // handle error }); let promise2 = fetch('https://api.example2.com') .then(response => response.json()) .then(data => { // process data }) .catch(error => { // handle error }); let allPromises = Promise.all([promise1, promise2]) .then(results => { // process results from both promises }) .catch(error => { // handle error if any promise failed });

In the above example, Promise.all ensures that both promises are executed, and we get the results from both of them once both are completed.

javascript
let promise1 = fetch('https://api.example1.com') .then(response => response.json()) .then(data => { // process data }) .catch(error => { // handle error }); let promise2 = fetch('https://api.example2.com') .then(response => response.json()) .then(data => { // process data }) .catch(error => { // handle error }); let racePromises = Promise.race([promise1, promise2]) .then(result => { // process result from the first resolved promise }) .catch(error => { // handle error if no promises were resolved });

In the second example, Promise.race returns the result from the first resolved promise. This can be useful when you want to stop waiting for other promises if the first one resolves successfully.

Quiz 🎯

Quick Quiz
Question 1 of 1

What does `Promise.all` return when one of the input promises rejects?

Quick Quiz
Question 1 of 1

What does `Promise.race` do in the given example?