diff options
Diffstat (limited to 'lib/internal/queue.js')
-rw-r--r-- | lib/internal/queue.js | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/lib/internal/queue.js b/lib/internal/queue.js index c73c322..44f1c01 100644 --- a/lib/internal/queue.js +++ b/lib/internal/queue.js @@ -70,11 +70,14 @@ export default function queue(worker, concurrency, payload) { if (q.idle()) { q.drain(); } - q.process(); + if (!sync) { + q.process(); + } }); } var workers = 0; + var sync = 0; var workersList = []; var q = { _tasks: new DLL(), @@ -120,7 +123,14 @@ export default function queue(worker, concurrency, payload) { } var cb = onlyOnce(_next(tasks)); + + // prevent stack growth when calling callback synchronously: + // unroll the recursion into a loop here. (The callback will + // have reduced the workers count synchronously, causing us to + // loop again) + sync = 1; worker(data, cb); + sync = 0; } }, length: function () { |