Sending API calls in batches Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Sending API calls in batches without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I’m currently trying to simulate half a million IoT devices to push payload to Azure IoT Hub using nodejs. Since node is multi-threaded in nature, its flooding iot hub with data and i am getting network errors.

I also tried async/await method but that is taking a lot of time to push data to IoT Hub.

Is there a way to only run 100 calls in parallel, wait for all of them to complete and then run the next 100 in node?

Much appreciated!

Answer

Build your batches as a nested array of Promises, then use Promise.all on each batch in a loop that awaits for each Promise.all to resolve.

// This is a mock request function, could be a `request` call 
// or a database query; whatever it is, it MUST return a Promise.
const sendRequest = () => {
  return new Promise((resolve) => {
    setTimeout(() => {
      console.log('request sent')
      resolve()
    }, 1000)
  })
}

// 5 batches * 2 requests = 10 requests.
const batches = Array(5).fill(Array(2).fill(sendRequest))

;(async function() {
  for (const batch of batches) {
    try {
      console.log('-- sending batch --')
      await Promise.all(batch.map(f => f()))  
    } catch(err) {
      console.error(err)
    }
  }
})()
We are here to answer your question about Sending API calls in batches - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji