Working with Multiple Promises
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. For example, 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.
JavaScript provides two options to deal with multiple asynchronous tasks:
- Let the tasks race and pick the first promise that resolves or rejects.
- Wait for all the tasks to resolve or for any one of them to reject.
Let’s explore each of these ...
Get Rediscovering JavaScript now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.