Reputation: 403
I am trying to use Promise.all of which, second promise is dependent on first promise response. Here is my code
let user = await userService.getByKey({ _id: params.userId });
let room = await matchService.findUserInRoom(user._id);
let match = await matchService.findMatch(params.userId);
But what now I was trying is to do it using Promise.all. Something like
let [user, room, match ] = await Promise.all([firstPromise, secondPromise, thirdPromise])
And I just don't know how to do it this way, tried a lot to find some reference but unable to. As because my second promise is dependent on the response of first promise just stuck how to pass this way
Upvotes: 0
Views: 1273
Reputation: 862
You can't call the first and second asynchronous API calls concurrently, but third one is independent so you can do it like the following:
async function getRoom(params) {
const user = await userService.getByKey({ _id: params.userId });
const room = await matchService.findUserInRoom(user._id);
return [user, room];
}
const [[user, room], match] = await Promise.all([
getRoom(params),
matchService.findMatch(params.userId)
]);
Upvotes: 3