Node.js while loop or setTimeout

Somebody told me its better to use while loops for such task like reading all the time account infos from a API or other things which are ongoing continuesly, he told me if you use setTimeout you will get one day stackoverflow problems, but i am not sure if this is just what he think or if it is really like that.

What do you recommend if i want to have something like a ongoing loop which will always read something from a external API?

I do not see a possibility to do this with a while loop without any setTimeout().

And if the duration of the interval is 1 or more minutes I would not do it in Nodejs itself but use a cron job.

for example i want to read the account balance of a exchange account every few seconds and update the response in a global objekt and i use now setTimeout to rekrusive call the function again and again, if i would use a do while loop it would block the other codes but some people say settimeout can create stackoverflow problems

a example with do while would look so:

do {

    await new Promise(resolve => setTimeout(resolve, 5000)); 
  } while (true);

indeed it also uses a settimeout but looks like not creating stackoverflow problems but it does block the code, what i use now looks like this:

function fetchDataFromAPI() {
  setTimeout(fetchDataFromAPI, 5000);


Main loop setInterval, setInterval method invokes something, while the main method chugs along… let the Interval worry about its own looping…

i am not sure, there are enough people which say use settimeout instead of setinterval so i dont know, settimeout people say does only make a new loop when the last one have finish and setinterval will always create a new loop, so i dont know i ask which method will be safe against stackoverflow creating

I mean, yes, a setInterval will run every X (milli)seconds, regardless of whether the previous task finished or not, but it will keep to a regular cadence. You could offset that with a flag/latch, if for some reason you think your process will take longer than your timer, but if you think that, why is your timer that short?

Did you get the permission of the API owner to do such heavy access on it? I do not know any API which is allowing that for free. Normally your IP gets banned after some time when you try to do that

1 Like

i just want to have something in my program which is always active like a loop but it should be safe against stackoverflow problems and here people say settimeout and setinterval can create such problem use do while but do while also have problems like they can block the process, so maybe one need to create seperate channel where only a do while loop runs and another channel where you have your routers for the website then it should not block each other

nearly every crypto exchange have rate limit of 10 request per second so i dont think that a request every 3 seconds is to much, if you want to show for example your account balance and equity on a website then reading every few seconds is the only way, i have already try websockets from some exchanges but they dont send you this changes

So what’s the use case? Right now, your setTimeout will wait 5 seconds between fetches, no matter how long the fetch took. If your desired goal is a pause between completions, then yeah, a Timeout is a better solution. If your goal is to fire a request every 5 seconds regardless of fetch time, an Interval is the better solution.

the question is which solution will be safe against stackoverflow problems, thats all, how to prevent such things for sure.

Fine. Since you dont want to actually engage with the situation, your answer is to do what you’re doing now.

A recursive setTimeout will not lead to an eventual stack overflow.

Normal recursion without a setTimeout can lead to a stack overflow because the portion of the stack for the original call needs to be maintained until after the recursive call returns.

Since setTimeout does not make the recursive call immediately, the original call is able to continue, return, and the stack gets cleaned up. The recursive call then runs later after the timeout period has elapsed.