javascript - Use async await with Array.map

Опубликовано: 28 Сентябрь 2024
на канале: Code Samples
94
2

Given the following code:
var arr = [1,2,3,4,5];

var results: number[] = await arr.map(async (item): Promisenumber = {
await callAsynchronousOperation(item);
return item + 1;
});

which produces the following error:

TS2322: Type 'Promisenumber[]' is not assignable to type 'number[]'.
Type 'Promisenumber is not assignable to type 'number'.

How can I fix it? How can I make async await and Array.map work together?
MDN docs for Promise.all: https://developer.mozilla.org/en-US/d...
Promise.allSettled: https://developer.mozilla.org/en-US/d...
Promise.any: https://developer.mozilla.org/en-US/d...
Promise.race: https://developer.mozilla.org/en-US/d...