I created a foreach loop which calls Facebook's API to fetch data. But since the number of calls are too high, it gives an error that JavaScript's maximum call stack size has exceeded and it stops there. I was thinking of delaying the execution of each iteration after 5 mins or a fix interval of time. I took help from this question. Now I want to know, does executing each iteration after an interval of time clear the JavaScript call stack or not?
Code:
var updateFeeds = function(){
db.collection("accounts").find({"status": "live"}).toArray(function(err, clients) {
var interval = 10 * 1000;
var i = 0;
clients.forEach(function(client, index, array){
setTimeout( function (i) {
i++;
****code*****
}, interval * i, i);
});
For each client there are almost 5000 request calls inside the code. I want to clear the JavaScript heap memory after completion of each iteration.
Error: FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory
Aborted (core dumped)