Skip to content

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

Closed
@baslr

Description

@baslr

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions