Using Promise.all to collect an array of async results
In the previous recipe, we saw how to trigger multiple asynchronous functions before awaiting their results. We've also seen how well the Promise API and asyc/await operators work together. There are some situations where it is preferable to use the Promise API.
In this recipe, we'll see how to use Promise.all to collect the result of multiple asynchronous operations.
Getting ready
This recipe assumes you already have a workspace that allows you to create and run ES modules in your browser. If you don't, please see the first two chapters.
How to do it...
- Open your command-line application and navigate to your workspace.
- Create a new folder named
04-06-Promise-all-collect-concurrently. - Create three functions,
checkEngines,checkFlightPlan, andcheckNavigationSystemthat log a message when they start and return aPromisethat resolves totrueif a random number is higher than a threshold after some timeout:
function checkEngines() {
console...