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

eachSeries with a nested async call results in maximum call stack size exceeded #1265

Closed
brainthinks opened this issue Aug 10, 2016 · 3 comments
Labels

Comments

@brainthinks
Copy link

brainthinks commented Aug 10, 2016

What version of async are you using?

2.0.1

Which environment did the issue occur in (Node version/browser version)

Node 6.2.1
NPM 3.9.3

What did you do? Please include a minimal reproducable case illustrating issue.

let counter = 2000;

async.eachSeries(
  new Array(counter),
  (item, cb) => {
    async.parallel([
      (pCb) => {
        console.log(counter--);
        pCb();
      },
    ], cb);
  },
  () => console.log('done')
);

What did you expect to happen?

I expected each of the iterations to occur, which would result in the last two console.log's being '1' and 'done'.

What was the actual result?

After fewer than 1000 iterations, I would receive this error:

RangeError: Maximum call stack size exceeded

When I reverted back to async 1.5.2, the above code worked as expected.

@clickclickonsal
Copy link

+1

1 similar comment
@jhyman2
Copy link

jhyman2 commented Aug 10, 2016

+1

@megawac
Copy link
Collaborator

megawac commented Aug 10, 2016

The reason this worked in async 1.5.2 is we ensured the callback was always called asynchronously in that version. In version 2 we removed this guard (see https://github.com/caolan/async/blob/master/intro.md#synchronous-iteration-functions)

So in your case to fix the script you can either use async.ensureAsync(cb) or async.setImmediate(cb) to work around the recursion issue:

var counter = 2000;

async.eachSeries(
  new Array(counter),
  (item, cb) => {
    async.parallel([
      (pCb) => {
        console.log(counter--);
        async.setImmediate(pCb);
      },
    ], cb);
  },
  () => console.log('done')
);

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

No branches or pull requests

4 participants