javascript - Use async await with Array.map

Published: 12 April 2024
on channel: 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...


Watch video javascript - Use async await with Array.map online without registration, duration hours minute second in high quality. This video was added by user Code Samples 12 April 2024, don't forget to share it with your friends and acquaintances, it has been viewed on our site 9 once and liked it people.