Search⌘ K
AI Features

Working with Multiple Promises: Racing Promises

Explore how to work with multiple asynchronous tasks in JavaScript by using the Promise race method. Understand how to pick the first promise to resolve or reject among several asynchronous operations, enabling efficient handling of tasks like selecting the best response quickly.

In the examples so far, promises were used to implement a single asynchronous task. In nontrivial scenarios, we may need multiple asynchronous tasks to be executed to solve a problem.

An analogy

A currency exchange broker may want to pick the best price quote from more than one provider. A promise carries the response for a single asynchronous task, but when dealing with multiple tasks, we need to combine data from multiple asynchronous functions, each returning a promise.

Handling multiple asynchronous functions

...