Promise.all
works great when waiting for a resolution for an array of homogenous items (same data). With Promise.all
the following is very convenient:
const items = await Promise.all(listOfPromises)
But suppose I have the following scenario:
const cars = await getListOfCars()
const trucks = await getListOfTrucks()
const airplanes = await getListOfAirplanes()
The above list of promises is not dependent on each other and can run in parallel. How can I await for all the promises to finish while maintaining their variable names?
Ideally, I should be able to do the following:
const cars = await getListOfCars()
const trucks = await getListOfTrucks()
const airplanes = await getListOfAirplanes()
await Promise.all([cars, trucks, airplanes])
// Once this is done, I'd like to be able to refer to the resolved promises by their variable names but that does not work since I suspect placing them into the array above copies them.
console.log(cars); // Still a promise instead of a value. I'd like for it to be the value
Who can I structure my code so that I can still refer to the variable names for the resolved values?