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

后端 未结 3 634
鱼传尺愫
鱼传尺愫 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:01

    If you are using a more recent version of javascript with async/await support, you can do the following:

    const array = ['asdf', 'foo', 'bar'];
    let users = [];
    for (const id in array) {
      const response = await axios('/user/' + id);
      users.push(response);
    }
    
    console.log(users);
    

提交回复
热议问题