Use async await with Array.map

Given the following code:

var arr = [1,2,3,4,5];

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

which produces the following error:

TS2322: Type ‘Promise<number>[]’ is not assignable to type ‘number[]’.
Type ‘Promise<number> is not assignable to type ‘number’.

How can I fix it? How can I make async await and Array.map work together?

10 Answers
10

Leave a Comment