3

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 completes.

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);
  });
dracarys
  • 1,071
  • 1
  • 15
  • 31
  • See this post: [Reading requests in sequence](https://stackoverflow.com/a/37576787/3257622) – Reyno Nov 16 '20 at 08:10

1 Answers1

4

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));
  }
  ...
}
michael
  • 4,053
  • 2
  • 12
  • 31
  • 1
    @dracarys you wrap the loop inside a [try/catch](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/try...catch) block – Reyno Nov 16 '20 at 08:16
  • As for the results, you can push each result to an array. `results.push(await axios.post());` for example. – eloyra Nov 16 '20 at 08:22