Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to fix async.eachSeries Bug RangeError: Maximum call stack size exceeded #590

Closed
baslr opened this issue Aug 6, 2014 · 2 comments
Closed

Comments

@baslr
Copy link

baslr commented Aug 6, 2014

Hi,

I run in a problem with arrays that have many items.
With the following code

  var a, async, i;

  async = require('async');

  a = (function() {
    var _i, _results;
    _results = [];
    for (i = _i = 1; _i <= 10; i = ++_i) {
      _results.push(i);
    }
    return _results;
  })();

  async.eachSeries(a, function(i, cb) {
    console.log(i);
    return cb();
  }, function() {
    return console.log('done');
  });

or

async = require 'async'

a = (i for i in [1..10])

async.eachSeries a, (i, cb) ->
  console.log i
  cb()
, () ->
  console.log 'done'

I get

…
4650
4651
4652

RangeError: Maximum call stack size exceeded```

with

    async.eachSeries = function (arr, interval, iterator, callback) {
        callback = callback || function () {};
        if (!arr.length) {
            return callback();
        }
        var intv  = undefined;
        var index = 0;
        var bDone = false;

        var iterate = function () {
            iterator(arr[index], function(err) {
                if(err) {
                    callback(err);
                    clearInterval(intv);
                } else {
                    index++;
                    bDone = true;
                }
            });
        };

        intv = setInterval( function() {
            if(index == arr.length && bDone) {
                clearInterval(intv);
                callback();
            } else if(bDone) {
                bDone = false;
                iterate();
            }
        }, interval);
        iterate();
    };

the RangeError is fixed. But the overall call is obviously slower. But in my case this is ok, because the iterator spawns a process and have to wait anyway.
Does it makes sense to intergrate this case as a addtional function or is there a async function that does my case?

Thanks for your help.

@baslr
Copy link
Author

baslr commented Aug 6, 2014

applyed #588 with setImmediate(iterate);

fixed.

@baslr baslr closed this as completed Aug 6, 2014
@born2net
Copy link

I confirm that applyed #588 with setImmediate(iterate); fixes the issue, can we get this to be put into main build?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants