0

I have the below piece of code which hits a url for each user id in loop.

var requests = [];
for (let i = 0; i < userids.length; i++) {
    requests.push(this.getUserData(authToken, userid[i]));
}
const userdetails=await (Promise as any).allSettled(requests);

The code fails for some requests and returns error for some requests. But when the same code is implememnted using sequential processing it works and returns correct data. Below is the code for the same

var requests:any=[]
for (let i = 0; i < userids.length; i++) {
    requests.push(await this.getUserData(authToken, userid[i]));
}

How shall I fix it?

monica_s
  • 65
  • 6
  • https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop – Sandip Nirmal Dec 23 '21 at 05:44
  • Fix whatever causes the error when there are concurrent requests? We don't know what `getUserData` does and how the server handling the request is implemented, so we can't help you with that. You haven't even told us *what* errors you get. – Bergi Dec 23 '21 at 08:02

0 Answers0