Reputation: 591
I have an array of values, and for each value i want to return an object that uses that value in a promise, i have the following code:
const arr= serieses.map(async x => {
const seriesId = await getSeriesIDFromName(x);
return { part_id: partID, door_series_id: seriesId[0]["id"] };
});
when i print arr after it all finished i get this
[
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> },
Promise { <pending> }
]
But if i print each value before the return statment, i get what i need,
{ part_id: 1, door_series_id: 1 }
{ part_id: 1, door_series_id: 12 }
{ part_id: 1, door_series_id: 15 }
{ part_id: 1, door_series_id: 16 }
{ part_id: 1, door_series_id: 8 }
{ part_id: 1, door_series_id: 6 },
how do i get arr to holed those values, and the the promise object?
Upvotes: 0
Views: 457
Reputation: 2385
You should use Promise.all()
for resolving all the promises in array, like this
const arr = serieses.map(async x => {
const seriesId = await getSeriesIDFromName(x);
return {part_id: partID, door_series_id: seriesId[0]["id"]};
});
const results = await Promise.all(arr)
results
will contain an array with the values resolved from each promise
Upvotes: 4