2

I've a problem with Axios and foreach loop. My Api provider support only 5 contemporary call so I want call one by one but when execute this code the each body not wait the the finish call function and receive the error code 429. How can resolve this? thanks.

async function call(url) {
  var options = {
    method: 'GET',
    url: url,
    auth: {
      username: '*****',
      password: '*****'
    }
  };
  var response = await axios.request(options);
  print(response.data["Id"])
}

app.get('/save', async (req, res) => {
  var options = {
    method: 'GET',
    url: 'getListUser',
    auth: {
      username: '***',
      password: '***'
    }
  };
  var response = await axios.request(options);

  response.data["users"].forEach( async (val) => {
    console.log("ENTER");
    var url = 'getDetailUser' + val["id"];
    var res = await call(url); // <- How to wait finish this?
    console.log("EXIT")
  }, (err) => {
      console.log(err)
  })
  res.status(200).send("ok").end();
});

3 Answers 3

2

FYI, Promise couldn't work with loop that involves callback ie forEach. Alternatively, you could use for of

try {
  for (const val of response.data['users']) {
    console.log("ENTER");
    var url = 'getDetailUser' + val["id"];
    var res = await call(url); 
    console.log("EXIT")
  }
} catch (error) {
  console.log(error)
}
Sign up to request clarification or add additional context in comments.

Comments

1

I would say answer by @Ifaruki is correct with a minor change

await Promise.allSettled(response.data["users"].map(val => {
   var url = 'getDetailUser' + val["id"];
   return call(url); 
}))

For details check the difference. In some cases Promise.all might work but if any of the Promise fails, the whole result of Promise.all will be a rejection.

The code 429 can be resolved by looking at 429 Too Many Requests

Comments

0

Promise.all() is the way

await Promise.all(response.data["users"].map(val => {
   var url = 'getDetailUser' + val["id"];
   return call(url); 
}))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.