javascript - Use async await with Array.map

Опубликовано: 12 Апрель 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...


Смотрите видео javascript - Use async await with Array.map онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь Code Samples 12 Апрель 2024, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 94 раз и оно понравилось 2 людям.