javascript - Send multiple axios requests in a for loop in series (One after the other) - Stack Overflow

Currently I am using Promise.all() to send multiple axios requests all at once, but I want to send the

Currently I am using Promise.all() to send multiple axios requests all at once, but I want to send the requests one by one, meaning send the second request after the first one pletes.

This is important so far as they timestamp of the POST request is important. The POST request for the promise in the 0th element of the array needs to have a earlier timestamp than the promise at the 1st element and so forth.

Here is the code I have written so far.

Promise.all(
  rowNodes.map((row) => {
    axios.post(url, row, axiosOptions);
  })
)
  .then((responseArr) => {
    console.log(responseArr);
  })
  .catch((errorArr) => {
    console.log(errorArr);
  });

Currently I am using Promise.all() to send multiple axios requests all at once, but I want to send the requests one by one, meaning send the second request after the first one pletes.

This is important so far as they timestamp of the POST request is important. The POST request for the promise in the 0th element of the array needs to have a earlier timestamp than the promise at the 1st element and so forth.

Here is the code I have written so far.

Promise.all(
  rowNodes.map((row) => {
    axios.post(url, row, axiosOptions);
  })
)
  .then((responseArr) => {
    console.log(responseArr);
  })
  .catch((errorArr) => {
    console.log(errorArr);
  });
Share Improve this question asked Nov 16, 2020 at 8:06 dracarysdracarys 1,1212 gold badges16 silver badges39 bronze badges 1
  • See this post: Reading requests in sequence – Reyno Commented Nov 16, 2020 at 8:10
Add a ment  | 

1 Answer 1

Reset to default 6

Please try to use for loop inside async/await function. Like below

const someFunc = async () => {
  ...
  const responses = [];
  for (let i = 0; i < rowNodes.length; i++) {
    responses.push(await axios.post(url, rowNodes[i], axiosOptions));
  }
  ...
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742397602a4436262.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信