Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delay between api calls Nodejs

I'm using Nodejs to implement a web app. I'm having a list of objects and I want to call a 3rd party API for each of these objects. The problem is the api has rate limit so based on my calculation I have to call the api every 1.5 sec. I tried using setTimeout method but it doesn't work in for loop. I also looked into Cron module but it doesn't help me since I only want to call api once per object. Can anyone help me with this. here is my code in sever side :

for(var obj in list)
{
      setTimeout(function() {
          apicall();
                }, 1500);
}
like image 915
Hirad Roshandel Avatar asked Jun 05 '15 22:06

Hirad Roshandel


People also ask

How do I add a delay in NodeJS?

One way to delay execution of a function in NodeJS is to use the seTimeout() function. Just put the code you want to delay in the callback.

Does NodeJS have setTimeout?

The setTimeout function is used to call a function after the specified number of milliseconds. The delay of the called function begins after the remaining statements in the script have finished executing. The setTimeout function is found in the Timers module of Node. js.

Is NodeJS good for API?

Node. js renders such wonderful support to developers for the development of API. A real-time API which is dynamic can be built using node. js.

How does setTimeout work in node?

The setTimeout() executes the function passed in the first argument after the time specified in the second argument. The setTimeout function doesn't block other code and the rest of the code is executed and after a specified time the code inside setTimeout function is executed.


2 Answers

The problem with for loops, or any loops is, that they are usually fast. In fact, it takes about several microseconds to loop over the list. This means that within a range of a few microseconds, you schedule several anonymous functions to be called after 1.5 seconds. This means that all the functions, even if delayed, will be fired almost all at once.

You need something that will cause the delays to increase over the course of the for loop.

A most basic setup would be to use a current index of the array as a multiplier.

for(var i in list) { // list is an array, i is current index
  setTimeout(function() {
    apicall()
  }, 1500 * i) // With each iteration, the delay increases
}

On a side note, in this basic example, you do not even need a closure:

setTimeout(apicall, 1500 * i)
like image 133
Robert Rossmann Avatar answered Sep 29 '22 05:09

Robert Rossmann


You could make a variable that increments time on each pass of the loop so your timeout goes from 1500, 3000, 4500 on each loop adding 1500 each time the reason yours isn't working is because that loop is creating however many timeouts all within milliseconds of each other because they're asynchronous calls, so after like 1600 ms you have however many intervals all going off at once.

like image 35
Datsik Avatar answered Sep 29 '22 05:09

Datsik