Sometimes it's possible to initiate multiple asynchronous operations at the same time. This can be desirable, for example, if multiple network requests are necessary in order to fetch all the data for a given page. Waiting for each request to finish before starting the next one wastes time.
In this recipe, we'll see how to use await
to initiate and wait for multiple results concurrently.
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.
- Open your command-line application and navigate to your workspace.
- Create a new folder named
04-05-await-concurrently
. - Create three functions,
checkEngines
,checkFlightPlan
, andcheckNavigationSystem
that log a message when they start and return aPromise
that resolves totrue
if a random number is higher than a threshold after some timeout:
function checkEngines() { console.log('checking...