Reputation: 87
I am trying to set some variables equal to the await result of a function, but I want the async functions to run concurrently.
my code:
async function track() {
var track = await getTrack(urlParams.get('track'))
var trackAnalysis = await getTrackAnalysis(urlParams.get('track'))
var artists = await getArtists(artistIds)
//then do something with all 3 variables after they have all been resolved
}
Upvotes: 1
Views: 49
Reputation: 707546
If you want them to run concurrently and they are actually asynchronous and return a promise that resolves with the appropriate value, then you can use Promise.all()
to run them concurrently:
async function track() {
let [track, trackAnalysis, artists] = await Promise.all([
getTrack(urlParams.get('track')),
getTrackAnalysis(urlParams.get('track')),
getArtists(artistIds)
]);
// do something with all 3 variables
}
Upvotes: 1