0

I have a set of requests but they can not be called all simultaneously so I decided to split the set into the chucks of 10. I am wondering how can I make 10 requests and wait for them all to complete like the example above:

data = []
for(mindex = 0; mindex < 1000; mindex = mindex + 10){
   request_chunk = []
   for(index = mindex+1; index < mindex+10; index++){
      request_chunk.push(api.call(requests[index]).getPromise();
   }

  data = data + waitPromiseToComplete(request_chunk);
}
chatzich
  • 1,083
  • 2
  • 11
  • 26

1 Answers1

1

You can use Promise.all and await:

(async function () {
  const data = []; // always declare variables!
  for(let mindex = 0; mindex < 1000; mindex = mindex + 10){
    const request_chunk = []
    for(let index = mindex + 1; index < mindex + 10; index++){
      request_chunk.push(api.call(requests[index]).getPromise();
    }

    data = data.concat(await Promise.all(request_chunk));
  }
})();
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151