How to chain async/await using for data that depends on the first call
Use an async
function for your map function and then use Promise.all
on that result.
async function getUsers() {
const users = await Api.getAllUsers()
return Promise.all(users.map(async (user) => {
return {
id: user.id,
group: await Api.getGroupByUserId(user.id),
}
}))
}
When you map
with an async function you essentially get an array of promises. That's why you need Promise.all