0

I need to process a quite large quantity of entries (tens of thousands) with functions such as async.each or async.eachSeries or async.whilst.

Almost reduntant to say that I have to use async because I am going to call some queries for each entry, making the code asynchronous.

Unfortunately, using async's functions, the call stack gets consumed pretty fast, leading to RangeError: Maximum call stack size exceeded.

Is there a way to tweak async in order to prevent this problem? An iterative implementation would solve the problem. Any alternative library that takes this problem into account?

here is an example code snippet that show the problem:

async = require('async');

a = []
for(i = 0; i < 10000000; i++) {
    a[i] = i;
}

async.eachSeries(a,function(element,callback) {
    console.log(element);
    callback();
},function(err){
    if(err){
        console.log("error:",err);
    }
    console.log("finished.");
});
fstab
  • 4,801
  • 8
  • 34
  • 66
  • No, but by using your own stack, you can usually restate recursive methods without recursion. Provide more code, and I'll provide an answer... – spender Dec 05 '14 at 15:50
  • Here is the code of the script, sorry if it's a bit messy, it's undergoing a refactoring: http://nopaste.info/ca0c118591.html – fstab Dec 05 '14 at 15:54

1 Answers1

0

What you are looking for is tail recursion. Check this question: What is tail recursion?

Community
  • 1
  • 1
IProblemFactory
  • 9,551
  • 8
  • 50
  • 66