There's more...

In fact, because the results are always resolved with a Promise, async functions can be resolved as a group using Promise.all. You can see an example of async functions with their results joined with a Promise.all:

async function checkEngines(threshold = 0.9) { 
  return Math.random() < threshold; 
} 
 
async function checkFlightPlan(threshold = 0.9) { 
  return Math.random() < threshold; 
} 
 
async function checkNavigationSystem(threshold = 0.9) { 
  return Math.random() < threshold; 
} 
 
Promise.all([ 
    checkEngines(), 
    checkFlightPlan(0.5), 
    checkNavigationSystem(0.75) 
]).then(function([enginesOk, flighPlanOk, navigationOk]) { 
  if (enginesOk) { 
    console.log('engines ready to go'); 
  } else { 
    console.error('engines not ready'); 
  } 
 
  if (flighPlanOk) { 
    console.log('flight plan good to go'); 
  } else { 
    console.error('error found in flight plan'); 
  } 
 
  if (navigationOk) { 
    console.log('navigation systems good to go'); 
  } else { 
    console.error('error found in navigation systems'); 
  } 
}) 

The preceding code works as expected. The functions can even be called directly with an argument, without needing to wrap them in a call to Promise.resolve.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.22.236.249