Use async await with Array.map

前端 未结 5 2116
臣服心动
臣服心动 2020-11-22 14:42

Given the following code:

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

var results: number[] = await arr.map(async (item): Promise => {
        await callAsynchr         


        
5条回答
  •  自闭症患者
    2020-11-22 15:33

    There's another solution for it if you are not using native Promises but Bluebird.

    You could also try using Promise.map(), mixing the array.map and Promise.all

    In you case:

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

提交回复
热议问题