Use async await with Array.map

前端 未结 5 2026
臣服心动
臣服心动 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:36

    I'd recommend using Promise.all as mentioned above, but if you really feel like avoiding that approach, you can do a for or any other loop:

    const arr = [1,2,3,4,5];
    let resultingArr = [];
    for (let i in arr){
      await callAsynchronousOperation(i);
      resultingArr.push(i + 1)
    }
    

提交回复
热议问题