Axios.get().then() in a for loop

后端 未结 3 619
鱼传尺愫
鱼传尺愫 2020-12-28 10:24

How would I go about running Axios in a for loop, each with a corresponding .then() function. Then after the for loop ends, run another function.

Exampl

3条回答
  •  一生所求
    2020-12-28 11:13

    You should collect all the promises inside an array and use promise.all in the following manner -

    const array = ['asdf', 'foo', 'bar'];
    let promises = [];
    for (i = 0; i < array.length; i++) {
      promises.push(axios.get('/user/' + array[i].id))
    }
    
    Promise.all(promises)
      .then(responses => console.log(responses));
    

提交回复
热议问题