From e0a51c362aad35fd6aad07c4dc463ba330deb634 Mon Sep 17 00:00:00 2001 From: Dominic Barnes Date: Fri, 10 Aug 2012 14:52:35 -0500 Subject: [PATCH 001/956] adding forEachOf to iterate objects asynchronously --- lib/async.js | 29 ++++++++++++++++++++++++++++ test/test-async.js | 47 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+) diff --git a/lib/async.js b/lib/async.js index 5f2fb189e..179918e97 100755 --- a/lib/async.js +++ b/lib/async.js @@ -57,6 +57,14 @@ return memo; }; + var _forEachOf = function (object, iterator) { + for (key in object) { + if (object.hasOwnProperty(key)) { + iterator(object[key], key); + } + } + }; + var _keys = function (obj) { if (Object.keys) { return Object.keys(obj); @@ -111,6 +119,27 @@ }); }; + async.forEachOf = function (object, iterator, callback) { + callback = callback || function () {}; + var completed = 0, size = _keys(object).length, key; + if (!size) { + return callback(); + } + _forEachOf(object, function (value, key) { + iterator(object[key], key, function (err) { + if (err) { + callback(err); + callback = function () {}; + } else { + completed += 1; + if (completed === size) { + callback(null); + } + } + }); + }); + }; + async.forEachSeries = function (arr, iterator, callback) { callback = callback || function () {}; if (!arr.length) { diff --git a/test/test-async.js b/test/test-async.js index 323527a99..e26e936c4 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -17,6 +17,13 @@ function forEachIterator(args, x, callback) { }, x*25); } +function forEachOfIterator(args, x, key, callback) { + setTimeout(function(){ + args.push(key, x); + callback(); + }, x*25); +} + function mapIterator(call_order, x, callback) { setTimeout(function(){ call_order.push(x); @@ -43,6 +50,13 @@ function forEachNoCallbackIterator(test, x, callback) { test.done(); } +function forEachOfNoCallbackIterator(test, x, key, callback) { + test.equal(x, 1); + test.equal(key, "a"); + callback(); + test.done(); +} + function getFunctionsObject(call_order) { return { one: function(callback){ @@ -652,6 +666,39 @@ exports['forEach no callback'] = function(test){ async.forEach([1], forEachNoCallbackIterator.bind(this, test)); }; +exports['forEachOf'] = function(test){ + var args = []; + async.forEachOf({ a: 1, b: 2 }, forEachOfIterator.bind(this, args), function(err){ + test.same(args, ["a", 1, "b", 2]); + test.done(); + }); +}; + +exports['forEachOf empty object'] = function(test){ + test.expect(1); + async.forEachOf({}, function(value, key, callback){ + test.ok(false, 'iterator should not be called'); + callback(); + }, function(err) { + test.ok(true, 'should call callback'); + }); + setTimeout(test.done, 25); +}; + +exports['forEachOf error'] = function(test){ + test.expect(1); + async.forEachOf({ a: 1, b: 2 }, function(value, key, callback) { + callback('error'); + }, function(err){ + test.equals(err, 'error'); + }); + setTimeout(test.done, 50); +}; + +exports['forEachOf no callback'] = function(test){ + async.forEachOf({ a: 1 }, forEachOfNoCallbackIterator.bind(this, test)); +}; + exports['forEachSeries'] = function(test){ var args = []; async.forEachSeries([1,3,2], forEachIterator.bind(this, args), function(err){ From 2a13d0857682663e556b1a344d8c33d3a6c289bf Mon Sep 17 00:00:00 2001 From: Dominic Barnes Date: Wed, 6 Feb 2013 22:01:50 -0600 Subject: [PATCH 002/956] adding forEachOfSeries and forEachOfLimit along with tests --- lib/async.js | 88 +++++++++++++++++++++++++- test/test-async.js | 151 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 235 insertions(+), 4 deletions(-) diff --git a/lib/async.js b/lib/async.js index 179918e97..f1ef20934 100755 --- a/lib/async.js +++ b/lib/async.js @@ -121,7 +121,8 @@ async.forEachOf = function (object, iterator, callback) { callback = callback || function () {}; - var completed = 0, size = _keys(object).length, key; + var size = object.length || _keys(object).length; + var completed = 0 if (!size) { return callback(); } @@ -173,6 +174,42 @@ iterate(); }; + async.forEachOfSeries = function (obj, iterator, callback) { + callback = callback || function () {}; + var keys = _keys(obj); + var size = keys.length; + if (!size) { + return callback(); + } + var completed = 0; + var iterate = function () { + var sync = true; + var key = keys[completed]; + iterator(obj[key], key, function (err) { + if (err) { + callback(err); + callback = function () {}; + } + else { + completed += 1; + if (completed >= size) { + callback(null); + } + else { + if (sync) { + async.nextTick(iterate); + } + else { + iterate(); + } + } + } + }); + sync = false; + }; + iterate(); + }; + async.forEachLimit = function (arr, limit, iterator, callback) { var fn = _forEachLimit(limit); fn.apply(null, [arr, iterator, callback]); @@ -219,6 +256,55 @@ }; + async.forEachOfLimit = function (obj, limit, iterator, callback) { + var fn = obj.constructor === Array ? _forEachOfLimit(limit) : _forEachOfLimit(limit); + fn.apply(null, [obj, iterator, callback]); + }; + + var _forEachOfLimit = function (limit) { + + return function (obj, iterator, callback) { + callback = callback || function () {}; + var keys = _keys(obj); + var size = keys.length; + if (!size || limit <= 0) { + return callback(); + } + var completed = 0; + var started = 0; + var running = 0; + + (function replenish () { + if (completed >= size) { + return callback(); + } + + while (running < limit && started < size) { + started += 1; + running += 1; + var key = keys[started - 1]; + iterator(obj[key], key, function (err) { + if (err) { + callback(err); + callback = function () {}; + } + else { + completed += 1; + running -= 1; + if (completed >= size) { + callback(); + } + else { + replenish(); + } + } + }); + } + })(); + }; + }; + + var doParallel = function (fn) { return function () { var args = Array.prototype.slice.call(arguments); diff --git a/test/test-async.js b/test/test-async.js index e26e936c4..cad8bb0ea 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -17,11 +17,11 @@ function forEachIterator(args, x, callback) { }, x*25); } -function forEachOfIterator(args, x, key, callback) { +function forEachOfIterator(args, value, key, callback) { setTimeout(function(){ - args.push(key, x); + args.push(key, value); callback(); - }, x*25); + }, value*25); } function mapIterator(call_order, x, callback) { @@ -699,6 +699,14 @@ exports['forEachOf no callback'] = function(test){ async.forEachOf({ a: 1 }, forEachOfNoCallbackIterator.bind(this, test)); }; +exports['forEachOf with array'] = function(test){ + var args = []; + async.forEachOf([ "a", "b" ], forEachOfIterator.bind(this, args), function(err){ + test.same(args, [0, "a", 1, "b"]); + test.done(); + }); +}; + exports['forEachSeries'] = function(test){ var args = []; async.forEachSeries([1,3,2], forEachIterator.bind(this, args), function(err){ @@ -735,6 +743,50 @@ exports['forEachSeries no callback'] = function(test){ async.forEachSeries([1], forEachNoCallbackIterator.bind(this, test)); }; +exports['forEachOfSeries'] = function(test){ + var args = []; + async.forEachOfSeries({ a: 1, b: 2 }, forEachOfIterator.bind(this, args), function(err){ + test.same(args, [ "a", 1, "b", 2 ]); + test.done(); + }); +}; + +exports['forEachOfSeries empty object'] = function(test){ + test.expect(1); + async.forEachOfSeries({}, function(x, callback){ + test.ok(false, 'iterator should not be called'); + callback(); + }, function(err){ + test.ok(true, 'should call callback'); + }); + setTimeout(test.done, 25); +}; + +exports['forEachOfSeries error'] = function(test){ + test.expect(2); + var call_order = []; + async.forEachOfSeries({ a: 1, b: 2 }, function(value, key, callback){ + call_order.push(value, key); + callback('error'); + }, function(err){ + test.same(call_order, [ 1, "a" ]); + test.equals(err, 'error'); + }); + setTimeout(test.done, 50); +}; + +exports['forEachOfSeries no callback'] = function(test){ + async.forEachOfSeries({ a: 1 }, forEachOfNoCallbackIterator.bind(this, test)); +}; + +exports['forEachOfSeries with array'] = function(test){ + var args = []; + async.forEachOfSeries([ "a", "b" ], forEachOfIterator.bind(this, args), function(err){ + test.same(args, [ 0, "a", 1, "b" ]); + test.done(); + }); +}; + exports['forEachLimit'] = function(test){ var args = []; var arr = [0,1,2,3,4,5,6,7,8,9]; @@ -822,6 +874,99 @@ exports['forEachLimit synchronous'] = function(test){ }); }; +exports['forEachOfLimit'] = function(test){ + var args = []; + var obj = { a: 1, b: 2, c: 3, d: 4 }; + async.forEachOfLimit(obj, 2, function(value, key, callback){ + setTimeout(function(){ + args.push(value, key); + callback(); + }, value * 5); + }, function(err){ + test.same(args, [ 1, "a", 2, "b", 3, "c", 4, "d" ]); + test.done(); + }); +}; + +exports['forEachOfLimit empty object'] = function(test){ + test.expect(1); + async.forEachOfLimit({}, 2, function(value, key, callback){ + test.ok(false, 'iterator should not be called'); + callback(); + }, function(err){ + test.ok(true, 'should call callback'); + }); + setTimeout(test.done, 25); +}; + +exports['forEachOfLimit limit exceeds size'] = function(test){ + var args = []; + var obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }; + async.forEachOfLimit(obj, 10, forEachOfIterator.bind(this, args), function(err){ + test.same(args, [ "a", 1, "b", 2, "c", 3, "d", 4, "e", 5 ]); + test.done(); + }); +}; + +exports['forEachOfLimit limit equal size'] = function(test){ + var args = []; + var obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }; + async.forEachOfLimit(obj, 5, forEachOfIterator.bind(this, args), function(err){ + test.same(args, [ "a", 1, "b", 2, "c", 3, "d", 4, "e", 5 ]); + test.done(); + }); +}; + +exports['forEachOfLimit zero limit'] = function(test){ + test.expect(1); + async.forEachOfLimit({ a: 1, b: 2 }, 0, function(x, callback){ + test.ok(false, 'iterator should not be called'); + callback(); + }, function(err){ + test.ok(true, 'should call callback'); + }); + setTimeout(test.done, 25); +}; + +exports['forEachOfLimit error'] = function(test){ + test.expect(2); + var obj = { a: 1, b: 2, c: 3, d: 4, e: 5 }; + var call_order = []; + + async.forEachOfLimit(obj, 3, function(value, key, callback){ + call_order.push(value, key); + if (value === 2) { + callback('error'); + } + }, function(err){ + test.same(call_order, [ 1, "a", 2, "b" ]); + test.equals(err, 'error'); + }); + setTimeout(test.done, 25); +}; + +exports['forEachOfLimit no callback'] = function(test){ + async.forEachOfLimit({ a: 1 }, 1, forEachOfNoCallbackIterator.bind(this, test)); +}; + +exports['forEachOfLimit synchronous'] = function(test){ + var args = []; + var obj = { a: 1, b: 2 }; + async.forEachOfLimit(obj, 5, forEachOfIterator.bind(this, args), function(err){ + test.same(args, [ "a", 1, "b", 2 ]); + test.done(); + }); +}; + +exports['forEachOfLimit with array'] = function(test){ + var args = []; + var arr = [ "a", "b" ] + async.forEachOfLimit(arr, 1, forEachOfIterator.bind(this, args), function (err) { + test.same(args, [ 0, "a", 1, "b" ]); + test.done(); + }); +}; + exports['map'] = function(test){ var call_order = []; async.map([1,3,2], mapIterator.bind(this, call_order), function(err, results){ From a2ac81ee875380a72e4f3cc3726f8523ff710404 Mon Sep 17 00:00:00 2001 From: Tom Boutell Date: Thu, 9 May 2013 12:28:16 -0400 Subject: [PATCH 003/956] Use of async.setImmediate prevents async.eachSeries from crashing the stack if the iterator function sometimes invokes the callback directly with 'return callback();' --- lib/async.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/async.js b/lib/async.js index 46f4f509e..a3215f324 100755 --- a/lib/async.js +++ b/lib/async.js @@ -136,7 +136,9 @@ callback(null); } else { - iterate(); + async.setImmediate(function() { + iterate(); + }); } } }); From 8e14a51cf11142c74fdbb818989bd34e23642129 Mon Sep 17 00:00:00 2001 From: nazomikan Date: Tue, 20 May 2014 01:06:56 +0900 Subject: [PATCH 004/956] add spec for #303 * waterfall call in another context * parallel call in another context * series call in another context --- test/test-async.js | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/test/test-async.js b/test/test-async.js index 79dd68006..bedc7ff41 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -751,6 +751,27 @@ exports['waterfall multiple callback calls'] = function(test){ async.waterfall(arr); }; +exports['waterfall call in another context'] = function(test) { + var vm = require('vm'); + var sandbox = { + async: async, + test: test + }; + + var fn = "(" + (function () { + async.waterfall([function (callback) { + callback(); + }], function (err) { + if (err) { + return test.done(err); + } + test.done(); + }); + }).toString() + "())"; + + vm.runInNewContext(fn, sandbox); +}; + exports['parallel'] = function(test){ var call_order = []; @@ -902,6 +923,28 @@ exports['parallel limit object'] = function(test){ }); }; +exports['parallel call in another context'] = function(test) { + var vm = require('vm'); + var sandbox = { + async: async, + test: test + }; + + var fn = "(" + (function () { + async.parallel([function (callback) { + callback(); + }], function (err) { + if (err) { + return test.done(err); + } + test.done(); + }); + }).toString() + "())"; + + vm.runInNewContext(fn, sandbox); +}; + + exports['series'] = function(test){ var call_order = []; async.series([ @@ -978,6 +1021,28 @@ exports['series object'] = function(test){ }); }; +exports['series call in another context'] = function(test) { + var vm = require('vm'); + var sandbox = { + async: async, + test: test + }; + + var fn = "(" + (function () { + async.series([function (callback) { + callback(); + }], function (err) { + if (err) { + return test.done(err); + } + test.done(); + }); + }).toString() + "())"; + + vm.runInNewContext(fn, sandbox); +}; + + exports['iterator'] = function(test){ var call_order = []; var iterator = async.iterator([ From 4ed6e83191912f43131829ccdf20aa870eda0232 Mon Sep 17 00:00:00 2001 From: Vaughn Iverson Date: Wed, 21 May 2014 01:42:47 -0700 Subject: [PATCH 005/956] Failing test for pausing with concurrrency --- test/test-async.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/test/test-async.js b/test/test-async.js index 79dd68006..6a4e8a37f 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -2421,6 +2421,51 @@ exports['queue pause'] = function(test) { }, 800); } +exports['queue pause with concurrency'] = function(test) { + var call_order = [], + task_timeout = 100, + pause_timeout = 50, + resume_timeout = 300, + tasks = [ 1, 2, 3, 4, 5, 6 ], + + elapsed = (function () { + var start = +Date.now(); + return function () { return Math.floor((+Date.now() - start) / 100) * 100; }; + })(); + + var q = async.queue(function (task, callback) { + setTimeout(function () { + call_order.push('process ' + task); + call_order.push('timeout ' + elapsed()); + callback(); + }, task_timeout); + }, 2); + + q.push(tasks); + + setTimeout(function () { + q.pause(); + test.equal(q.paused, true); + }, pause_timeout); + + setTimeout(function () { + q.resume(); + test.equal(q.paused, false); + }, resume_timeout); + + setTimeout(function () { + test.same(call_order, [ + 'process 1', 'timeout 100', + 'process 2', 'timeout 100', + 'process 3', 'timeout 400', + 'process 4', 'timeout 400', + 'process 5', 'timeout 500', + 'process 6', 'timeout 500' + ]); + test.done(); + }, 800); +} + exports['queue kill'] = function (test) { var q = async.queue(function (task, callback) { setTimeout(function () { From 657bad7e1751f0d9cb18c874a7a2c629a8be79c0 Mon Sep 17 00:00:00 2001 From: Vaughn Iverson Date: Wed, 21 May 2014 01:43:43 -0700 Subject: [PATCH 006/956] Remove trailing spaces --- lib/async.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/async.js b/lib/async.js index 01e8afcc4..1077aafc4 100755 --- a/lib/async.js +++ b/lib/async.js @@ -831,13 +831,13 @@ }; return q; }; - + async.priorityQueue = function (worker, concurrency) { - + function _compareTasks(a, b){ return a.priority - b.priority; }; - + function _binarySearch(sequence, item, compare) { var beg = -1, end = sequence.length - 1; @@ -851,7 +851,7 @@ } return beg; } - + function _insert(q, data, priority, callback) { if (!q.started){ q.started = true; @@ -873,7 +873,7 @@ priority: priority, callback: typeof callback === 'function' ? callback : null }; - + q.tasks.splice(_binarySearch(q.tasks, item, _compareTasks) + 1, 0, item); if (q.saturated && q.tasks.length === q.concurrency) { @@ -882,15 +882,15 @@ async.setImmediate(q.process); }); } - + // Start with a normal queue var q = async.queue(worker, concurrency); - + // Override push to accept second parameter representing priority q.push = function (data, priority, callback) { _insert(q, data, priority, callback); }; - + // Remove unshift function delete q.unshift; From f5d804f068e9950a38e34c050842cf2400cc1c00 Mon Sep 17 00:00:00 2001 From: Vaughn Iverson Date: Wed, 21 May 2014 01:52:03 -0700 Subject: [PATCH 007/956] Fixes loss of concurrency after pausing an async.queue(...) --- lib/async.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/async.js b/lib/async.js index 1077aafc4..a39e8c1cd 100755 --- a/lib/async.js +++ b/lib/async.js @@ -821,12 +821,15 @@ pause: function () { if (q.paused === true) { return; } q.paused = true; - q.process(); }, resume: function () { if (q.paused === false) { return; } q.paused = false; - q.process(); + // Need to call q.process once per concurrent + // worker to preserve full concurrency after pause + for (w = 1; w <= q.concurrency; w++) { + async.setImmediate(q.process); + } } }; return q; From 2f8680c6bd96a0ba7b38f052d200f6364a02de69 Mon Sep 17 00:00:00 2001 From: Vaughn Iverson Date: Thu, 22 May 2014 10:54:23 -0700 Subject: [PATCH 008/956] Add var to declare loop index as local --- lib/async.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/async.js b/lib/async.js index a39e8c1cd..a13f83520 100755 --- a/lib/async.js +++ b/lib/async.js @@ -827,7 +827,7 @@ q.paused = false; // Need to call q.process once per concurrent // worker to preserve full concurrency after pause - for (w = 1; w <= q.concurrency; w++) { + for (var w = 1; w <= q.concurrency; w++) { async.setImmediate(q.process); } } From e33735c6debaac77bc815443393ec29a5001d4d4 Mon Sep 17 00:00:00 2001 From: Dmitriy Schekhovtsov Date: Fri, 23 May 2014 09:15:53 +0300 Subject: [PATCH 009/956] Minor misprints fix --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0bea5311a..8f4e98c03 100644 --- a/README.md +++ b/README.md @@ -182,7 +182,7 @@ __Arguments__ * `arr` - An array to iterate over. * `iterator(item, callback)` - A function to apply to each item in `arr`. The iterator is passed a `callback(err)` which must be called once it has - completed. If no error has occured, the `callback` should be run without + completed. If no error has occurred, the `callback` should be run without arguments or with an explicit `null` argument. * `callback(err)` - A callback which is called when all `iterator` functions have finished, or an error occurs. @@ -256,7 +256,7 @@ __Arguments__ * `limit` - The maximum number of `iterator`s to run at any time. * `iterator(item, callback)` - A function to apply to each item in `arr`. The iterator is passed a `callback(err)` which must be called once it has - completed. If no error has occured, the callback should be run without + completed. If no error has occurred, the callback should be run without arguments or with an explicit `null` argument. * `callback(err)` - A callback which is called when all `iterator` functions have finished, or an error occurs. @@ -280,7 +280,7 @@ async.eachLimit(documents, 20, requestApi, function(err){ Produces a new array of values by mapping each value in `arr` through the `iterator` function. The `iterator` is called with an item from `arr` and a callback for when it has finished processing. Each of these callback takes 2 arguments: -an `error`, and the transformed item from `arr`. If `iterator` passes an error to this +an `error`, and the transformed item from `arr`. If `iterator` passes an error to his callback, the main `callback` (for the `map` function) is immediately called with the error. Note, that since this function applies the `iterator` to each item in parallel, From 4cb992f7887ec1b414e356db1fe3a8de882ab70e Mon Sep 17 00:00:00 2001 From: nazomikan Date: Sat, 24 May 2014 05:00:43 +0900 Subject: [PATCH 010/956] detect a browser environment and not attempt to run of require vm spec. --- test/test-async.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/test-async.js b/test/test-async.js index bedc7ff41..ddf2916f6 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -752,6 +752,12 @@ exports['waterfall multiple callback calls'] = function(test){ }; exports['waterfall call in another context'] = function(test) { + if (typeof process === 'undefined') { + // node only test + test.done(); + return; + } + var vm = require('vm'); var sandbox = { async: async, @@ -924,6 +930,11 @@ exports['parallel limit object'] = function(test){ }; exports['parallel call in another context'] = function(test) { + if (typeof process === 'undefined') { + // node only test + test.done(); + return; + } var vm = require('vm'); var sandbox = { async: async, @@ -1022,6 +1033,11 @@ exports['series object'] = function(test){ }; exports['series call in another context'] = function(test) { + if (typeof process === 'undefined') { + // node only test + test.done(); + return; + } var vm = require('vm'); var sandbox = { async: async, From ec7a11f01e842f9241a27def203922d4a1b9841e Mon Sep 17 00:00:00 2001 From: Sean McCann Date: Wed, 28 May 2014 23:43:24 -0400 Subject: [PATCH 011/956] Correct spelling of 'An' --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8f4e98c03..28cd3f71a 100644 --- a/README.md +++ b/README.md @@ -1551,7 +1551,7 @@ by `memoize`. __Arguments__ * `fn` - The function to proxy and cache results from. -* `hasher` - Tn optional function for generating a custom hash for storing +* `hasher` - An optional function for generating a custom hash for storing results. It has all the arguments applied to it apart from the callback, and must be synchronous. From ac463aab272417fbd5bec89f1a713f17ef1be47e Mon Sep 17 00:00:00 2001 From: lion-man Date: Sat, 31 May 2014 21:31:47 +0900 Subject: [PATCH 012/956] Add jshintrc --- .jshintrc | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 .jshintrc diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 000000000..3a2825a2e --- /dev/null +++ b/.jshintrc @@ -0,0 +1,19 @@ +{ + // Enforcing options + "eqeqeq": false, + "forin": true, + "indent": 4, + "noarg": true, + "undef": true, + "unused": true, + "trailing": true, + + // Relaxing options + "asi": false, + "eqnull": true, + "evil": true, + "expr": false, + "laxcomma": true, + "loopfunc": true, + "sub": true +} From a1a09dbeb742f2e2da17ac9aa013dc4bc5da3a2f Mon Sep 17 00:00:00 2001 From: jsdevel Date: Thu, 3 Jul 2014 05:05:45 -0700 Subject: [PATCH 013/956] chmod -x on lib/async.js --- lib/async.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 lib/async.js diff --git a/lib/async.js b/lib/async.js old mode 100755 new mode 100644 From 3a2b4481a6c0596f26c463427133fa6acf5e163c Mon Sep 17 00:00:00 2001 From: David Leonard Date: Sat, 16 Aug 2014 15:26:39 -0400 Subject: [PATCH 014/956] Updating download section --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8f4e98c03..f900495eb 100644 --- a/README.md +++ b/README.md @@ -80,11 +80,15 @@ async.map([1, 2, 3], AsyncSquaringLibrary.square.bind(AsyncSquaringLibrary), fun ## Download The source is available for download from -[GitHub](http://github.com/caolan/async). +[GitHub](https://github.com/caolan/async/blob/master/lib/async.js). Alternatively, you can install using Node Package Manager (`npm`): npm install async +As well as using Bower: + + bower install async + __Development:__ [async.js](https://github.com/caolan/async/raw/master/lib/async.js) - 29.6kb Uncompressed ## In the Browser From e56a37170b017c3bade81eedab57a99a5e2f019f Mon Sep 17 00:00:00 2001 From: Steve Robb Date: Sun, 17 Aug 2014 14:38:14 +0100 Subject: [PATCH 015/956] autoInject - dependency injection for auto. --- README.md | 68 +++++++++++++++++++ lib/async.js | 34 ++++++++++ test/test-async.js | 165 ++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 265 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8f4e98c03..60ed04a51 100644 --- a/README.md +++ b/README.md @@ -147,6 +147,7 @@ Usage: * [`priorityQueue`](#priorityQueue) * [`cargo`](#cargo) * [`auto`](#auto) +* [`autoInject`](#autoInject) * [`retry`](#retry) * [`iterator`](#iterator) * [`apply`](#apply) @@ -1342,6 +1343,73 @@ For a complicated series of `async` tasks, using the [`auto`](#auto) function ma new tasks much easier (and the code more readable). +--------------------------------------- + + +### autoInject(tasks, [callback]) + +A dependency-injected version of the [`auto`](#auto) function. Dependent tasks are +specified as parameters to the function, after the usual callback parameter, with the +parameter names matching the names of the tasks it depends on. This can provide even more +readable task graphs which can be easier to maintain. + +If a final callback is specified, the task results are similarly injected, specified as +named parameters after the initial error parameter. + +The autoInject function is purely syntactic sugar and its semantics are otherwise +equivalent to [`auto`](#auto). + +__Arguments__ + +* `tasks` - An object, each of whose properties is a function of the form + 'func(callback, [dependencies...]). The object's key of a property serves as the + name of the task defined by that property, i.e. can be used when specifying requirements + for other tasks. + The `callback` parameter is a `callback(err, result)` which must be called when finished, + passing an `error` (which can be `null`) and the result of the function's execution. + The remaining parameters name other tasks on which the task is dependent, and the results + from those tasks are the arguments of those parameters. +* `callback(err, [results...])` - An optional callback which is called when all the + tasks have been completed. It receives the `err` argument if any `tasks` + pass an error to their callback. The remaining parameters are task names whose results + you are interested in. This callback will only be called when all tasks have finished or + an error has occurred, and so do not not specify dependencies in the same way as `tasks` + do. If an error occurs, no further `tasks` will be performed, and `results` will only be + valid for those tasks which managed to complete. + + +__Example__ + +The example from [`auto`](#auto) can be rewritten as follows: + +```js +async.autoInject({ + get_data: function(callback){ + // async code to get some data + callback(null, 'data', 'converted to array'); + }, + make_folder: function(callback){ + // async code to create a directory to store a file in + // this is run at the same time as getting the data + callback(null, 'folder'); + }, + write_file: function(callback, get_data, make_folder){ + // once there is some data and the directory exists, + // write the data to a file in the directory + callback(null, 'filename'); + }, + email_link: function(callback, write_file){ + // once the file is written let's email a link to it... + // write_file contains the filename returned by write_file. + callback(null, {'file':write_file, 'email':'user@example.com'}); + } +}, function(err, email_link) { + console.log('err = ', err); + console.log('email_link = ', email_link); +}); +``` + + --------------------------------------- diff --git a/lib/async.js b/lib/async.js index a13f83520..4d2d69f28 100755 --- a/lib/async.js +++ b/lib/async.js @@ -498,6 +498,40 @@ }); }; + var _argsRegEx = /^function\s*[^\(]*\(\s*([^\)]*)\)/m; + var _diArgs = function(func) { + var result = func.toString().match(_argsRegEx)[1].split(','); + for (var i = 0, e = result.length; i != e; ++i) + result[i] = result[i].trim(); + return result; + }; + + async.autoInject = function(tasks, callback) { + var injTasks = {}; + for (var i in tasks) + injTasks[i] = (function(val) { + if (!(val instanceof Function)) + return val; + + var args = _diArgs(val); + if (args.length < 2) + return val; + + args.shift(); + + return args.concat(function(cb, results) { + val.apply(null, [cb].concat(_map(args, function(a) { return results[a]; }))); + }); + })(tasks[i]); + + return this.auto( + injTasks, + callback && function(err, results) { + callback.apply(null, [err].concat(_map(_diArgs(callback).slice(1), function(a) { return results[a]; }))); + } + ); + }; + async.retry = function(times, task, callback) { var DEFAULT_TIMES = 5; var attempts = []; diff --git a/test/test-async.js b/test/test-async.js index 6a3560694..81801199c 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -454,7 +454,6 @@ exports['auto results'] = function(test){ }); }; - exports['auto empty object'] = function(test){ async.auto({}, function(err){ test.done(); @@ -504,7 +503,169 @@ exports['auto error should pass partial results'] = function(test) { test.equals(err, 'testerror'); test.equals(results.task1, 'result1'); test.equals(results.task2, 'result2'); - test.done(); + test.done(); + }); +}; + +exports['autoInject'] = function(test){ + var callOrder = []; + var testdata = [{test: 'test'}]; + async.autoInject({ + task1: function(callback, task2){ + setTimeout(function(){ + callOrder.push('task1'); + callback(); + }, 25); + }, + task2: function(callback){ + setTimeout(function(){ + callOrder.push('task2'); + callback(); + }, 50); + }, + task3: function(callback, task2){ + callOrder.push('task3'); + callback(); + }, + task4: function(callback, task1, task2){ + callOrder.push('task4'); + callback(); + }, + task5: function(callback, task2){ + setTimeout(function(){ + callOrder.push('task5'); + callback(); + }, 0); + }, + task6: function(callback, task2){ + callOrder.push('task6'); + callback(); + } + }, + function(err){ + test.same(callOrder, ['task2','task6','task3','task5','task1','task4']); + test.done(); + }); +}; + +exports['autoInject petrify'] = function (test) { + var callOrder = []; + async.autoInject({ + task1: function (callback, task2) { + setTimeout(function () { + callOrder.push('task1'); + callback(); + }, 100); + }, + task2: function (callback) { + setTimeout(function () { + callOrder.push('task2'); + callback(); + }, 200); + }, + task3: function (callback, task2) { + callOrder.push('task3'); + callback(); + }, + task4: function (callback, task1, task2) { + callOrder.push('task4'); + callback(); + } + }, + function (err) { + test.same(callOrder, ['task2', 'task3', 'task1', 'task4']); + test.done(); + }); +}; + +exports['autoInject results'] = function(test){ + var callOrder = []; + async.autoInject({ + task1: function(callback, task2){ + test.same(task2, 'task2'); + setTimeout(function(){ + callOrder.push('task1'); + callback(null, 'task1a', 'task1b'); + }, 25); + }, + task2: function(callback){ + setTimeout(function(){ + callOrder.push('task2'); + callback(null, 'task2'); + }, 50); + }, + task3: function(callback, task2){ + test.same(task2, 'task2'); + callOrder.push('task3'); + callback(null); + }, + task4: function(callback, task1, task2){ + test.same(task1, ['task1a','task1b']); + test.same(task2, 'task2'); + callOrder.push('task4'); + callback(null, 'task4'); + } + }, + function(err, task2, task4, task3, task1){ + test.same(callOrder, ['task2','task3','task1','task4']); + test.same(task1, ['task1a','task1b']); + test.same(task2, 'task2'); + test.same(task3, undefined); + test.same(task4, 'task4'); + test.done(); + }); +}; + +exports['autoInject empty object'] = function(test){ + async.autoInject({}, function(err){ + test.done(); + }); +}; + +exports['autoInject error'] = function(test){ + test.expect(1); + async.autoInject({ + task1: function(callback){ + callback('testerror'); + }, + task2: function(callback, task1){ + test.ok(false, 'task2 should not be called'); + callback(); + }, + task3: function(callback){ + callback('testerror2'); + } + }, + function(err){ + test.equals(err, 'testerror'); + }); + setTimeout(test.done, 100); +}; + +exports['autoInject no callback'] = function(test){ + async.autoInject({ + task1: function(callback){callback();}, + task2: function(callback, task1){callback(); test.done();} + }); +}; + +exports['autoInject error should pass partial results'] = function(test) { + async.autoInject({ + task1: function(callback){ + callback(false, 'result1'); + }, + task2: function(callback, task1){ + callback('testerror', 'result2'); + }, + task3: function(callback, task2){ + test.ok(false, 'task3 should not be called'); + } + }, + function(err, task1, task2){ + test.equals(err, 'testerror'); + test.equals(task1, 'result1'); + test.equals(task2, 'result2'); + test.done(); }); }; From 2f17fcb52de3575183f5899d8cf6f4f58b1ecfc9 Mon Sep 17 00:00:00 2001 From: Jeremy Brudvik Date: Fri, 5 Sep 2014 15:58:00 -0700 Subject: [PATCH 016/956] Add npm badge to README Add npm badge to README for easy access to npmjs.org link and npm version --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 8f4e98c03..09f3c497f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Async.js [![Build Status via Travis CI](https://travis-ci.org/caolan/async.svg?branch=master)](https://travis-ci.org/caolan/async) +[![NPM version](http://img.shields.io/npm/v/async.svg)](https://www.npmjs.org/package/async) Async is a utility module which provides straight-forward, powerful functions From 9e1ba14ef4e784a408aacdeb499e7f72b7623227 Mon Sep 17 00:00:00 2001 From: Qix Date: Mon, 15 Sep 2014 18:02:50 -0600 Subject: [PATCH 017/956] improved seq() documentation wording Two changes were made here: - `latter` was changed to `former` (that caused a good deal of headache the other day) - added some clarification as to what exactly differs from the `compose()` function Since it's not entirely clear that `compose()` doesn't work left to right, `seq()` should better indicate that it does. --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8f4e98c03..55cea7340 100644 --- a/README.md +++ b/README.md @@ -972,7 +972,8 @@ add1mul3(4, function (err, result) { ### seq(fn1, fn2...) Version of the compose function that is more natural to read. -Each following function consumes the return value of the latter function. +Each following function consumes the return value of the former function. +It is the equivalent of the compose function with reversed arguments. Each function is executed with the `this` binding of the composed function. From 0d2f15046f96e66707a6ba5c3c60a9a8df510e14 Mon Sep 17 00:00:00 2001 From: Vitaly Aminev Date: Thu, 18 Sep 2014 21:16:05 +0400 Subject: [PATCH 018/956] Update `applyEachSeries` function signature to match `applyEach` so it's not confusing what it does --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8f4e98c03..4983906d1 100644 --- a/README.md +++ b/README.md @@ -1047,7 +1047,7 @@ async.each( --------------------------------------- -### applyEachSeries(arr, iterator, callback) +### applyEachSeries(arr, args..., callback) The same as [`applyEach`](#applyEach) only the functions are applied in series. From e4527c94362f6cbec0c965944a7b0462fd480f1b Mon Sep 17 00:00:00 2001 From: Tom Boutell Date: Mon, 22 Sep 2014 17:00:32 -0400 Subject: [PATCH 019/956] Document behavior of the default hash function of async.memoize --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 257def334..2f8c463f0 100644 --- a/README.md +++ b/README.md @@ -1313,6 +1313,8 @@ Caches the results of an async function. When creating a hash to store function results against, the callback is omitted from the hash and an optional hash function can be used. +If no hash function is specified, the first argument is used as a hash key, which may work reasonably if it is a string or a data type that converts to a distinct string. Note that objects and arrays will not behave reasonably. Neither will cases where the other arguments are significant. In such cases, specify your own hash function. + The cache of results is exposed as the `memo` property of the function returned by `memoize`. From cdddd3491461e479ab4f70c8f99df216b52f100d Mon Sep 17 00:00:00 2001 From: Tom Boutell Date: Mon, 22 Sep 2014 17:02:45 -0400 Subject: [PATCH 020/956] Back out an earlier rejected pull request --- lib/async.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/async.js b/lib/async.js index a3215f324..46f4f509e 100755 --- a/lib/async.js +++ b/lib/async.js @@ -136,9 +136,7 @@ callback(null); } else { - async.setImmediate(function() { - iterate(); - }); + iterate(); } } }); From 2a9145a93fe783ab62502b8be761d1c89651a4e4 Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Mon, 13 Oct 2014 10:53:26 -0700 Subject: [PATCH 021/956] Improve documentation for async.seq - seq requires the last argument to be a function - seq short-circuits on errors which means using handleError is redundant --- README.md | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 8f4e98c03..49189b29c 100644 --- a/README.md +++ b/README.md @@ -989,28 +989,21 @@ __Example__ // This example uses `seq` function to avoid overnesting and error // handling clutter. app.get('/cats', function(request, response) { - function handleError(err, data, callback) { - if (err) { - console.error(err); - response.json({ status: 'error', message: err.message }); - } - else { - callback(data); - } - } var User = request.models.User; async.seq( _.bind(User.get, User), // 'User.get' has signature (id, callback(err, data)) - handleError, function(user, fn) { user.getCats(fn); // 'getCats' has signature (callback(err, data)) - }, - handleError, - function(cats) { + } + )(req.session.user_id, function (err, cats) { + if (err) { + console.error(err); + response.json({ status: 'error', message: err.message }); + } else { response.json({ status: 'ok', message: 'Cats found', data: cats }); } - )(req.session.user_id); - } + }); +} }); ``` From e3c77ec05e1dc5c6df3e45b66b7a9add7799ce75 Mon Sep 17 00:00:00 2001 From: Anup Bishnoi Date: Tue, 28 Oct 2014 20:15:13 -0400 Subject: [PATCH 022/956] Fix incorrect sample code --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8f4e98c03..fdfdae5e8 100644 --- a/README.md +++ b/README.md @@ -536,14 +536,14 @@ By modifying the callback parameter the sorting order can be influenced: ```js //ascending order async.sortBy([1,9,3,5], function(x, callback){ - callback(err, x); + callback(null, x); }, function(err,result){ //result callback } ); //descending order async.sortBy([1,9,3,5], function(x, callback){ - callback(err, x*-1); //<- x*-1 instead of x, turns the order around + callback(null, x*-1); //<- x*-1 instead of x, turns the order around }, function(err,result){ //result callback } ); From 1365aaa560595a1b1477f0e97d50436dcd424db7 Mon Sep 17 00:00:00 2001 From: Ryan Copley Date: Thu, 13 Nov 2014 18:38:11 -0500 Subject: [PATCH 023/956] Removed the slower forEach in async.each --- lib/async.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/lib/async.js b/lib/async.js index a13f83520..394c41cad 100755 --- a/lib/async.js +++ b/lib/async.js @@ -42,9 +42,6 @@ }; var _each = function (arr, iterator) { - if (arr.forEach) { - return arr.forEach(iterator); - } for (var i = 0; i < arr.length; i += 1) { iterator(arr[i], i, arr); } From 2410077e26d5429ac45533438f008706dcb989f7 Mon Sep 17 00:00:00 2001 From: Mickael van der Beek Date: Tue, 18 Nov 2014 21:20:56 +0100 Subject: [PATCH 024/956] Fixes caolan/async#263 and other dependency issues by detecting dead-locks and throwing an Error. --- lib/async.js | 11 +++++++++++ test/test-async.js | 27 +++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/lib/async.js b/lib/async.js index a13f83520..50f7a2806 100755 --- a/lib/async.js +++ b/lib/async.js @@ -478,6 +478,17 @@ } }; var requires = task.slice(0, Math.abs(task.length - 1)) || []; + // prevent dead-locks + var len = requires.length; + var dep; + while (len--) { + if (!(dep = tasks[requires[len]])) { + throw new Error('Has inexistant dependency'); + } + if (_isArray(dep) && !!~dep.indexOf(k)) { + throw new Error('Has cyclic dependencies'); + } + } var ready = function () { return _reduce(requires, function (a, x) { return (a && results.hasOwnProperty(x)); diff --git a/test/test-async.js b/test/test-async.js index 6a3560694..8ee2bd903 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -580,6 +580,33 @@ exports['auto modifying results causes final callback to run early'] = function( }); }; +// Issue 263 on github: https://github.com/caolan/async/issues/263 +exports['auto prevent dead-locks due to inexistant dependencies'] = function(test) { + test.throws(function () { + async.auto({ + task1: ['noexist', function(callback, results){ + callback(null, 'task1'); + }] + }); + }, Error); + test.done(); +}; + +// Issue 263 on github: https://github.com/caolan/async/issues/263 +exports['auto prevent dead-locks due to cyclic dependencies'] = function(test) { + test.throws(function () { + async.auto({ + task3: ['task4', function(callback, results){ + callback(null, 'task3'); + }], + task4: ['task3', function(callback, results){ + callback(null, 'task4'); + }] + }); + }, Error); + test.done(); +}; + // Issue 306 on github: https://github.com/caolan/async/issues/306 exports['retry when attempt succeeds'] = function(test) { var failed = 3 From 74819d24051d1457c73ab301d697ec8b467115f8 Mon Sep 17 00:00:00 2001 From: Mickael van der Beek Date: Tue, 18 Nov 2014 21:24:02 +0100 Subject: [PATCH 025/956] Corrected task name typo for consistency with other tests. --- test/test-async.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test-async.js b/test/test-async.js index 8ee2bd903..a84cb4907 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -596,11 +596,11 @@ exports['auto prevent dead-locks due to inexistant dependencies'] = function(tes exports['auto prevent dead-locks due to cyclic dependencies'] = function(test) { test.throws(function () { async.auto({ - task3: ['task4', function(callback, results){ - callback(null, 'task3'); + task1: ['task2', function(callback, results){ + callback(null, 'task1'); }], - task4: ['task3', function(callback, results){ - callback(null, 'task4'); + task2: ['task1', function(callback, results){ + callback(null, 'task2'); }] }); }, Error); From 85c45eeac5b9c757067020be151de59996095d6b Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Mon, 13 Oct 2014 10:53:26 -0700 Subject: [PATCH 026/956] Improve documentation for async.seq - seq requires the last argument to be a function - seq short-circuits on errors which means using handleError is redundant --- README.md | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 55cea7340..550eb20be 100644 --- a/README.md +++ b/README.md @@ -990,28 +990,21 @@ __Example__ // This example uses `seq` function to avoid overnesting and error // handling clutter. app.get('/cats', function(request, response) { - function handleError(err, data, callback) { - if (err) { - console.error(err); - response.json({ status: 'error', message: err.message }); - } - else { - callback(data); - } - } var User = request.models.User; async.seq( _.bind(User.get, User), // 'User.get' has signature (id, callback(err, data)) - handleError, function(user, fn) { user.getCats(fn); // 'getCats' has signature (callback(err, data)) - }, - handleError, - function(cats) { + } + )(req.session.user_id, function (err, cats) { + if (err) { + console.error(err); + response.json({ status: 'error', message: err.message }); + } else { response.json({ status: 'ok', message: 'Cats found', data: cats }); } - )(req.session.user_id); - } + }); +} }); ``` From 7405049e28af17cd8126cf0231b9ed21e868fea6 Mon Sep 17 00:00:00 2001 From: Anup Bishnoi Date: Tue, 28 Oct 2014 20:15:13 -0400 Subject: [PATCH 027/956] Fix incorrect sample code --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 550eb20be..9ae16b27d 100644 --- a/README.md +++ b/README.md @@ -536,14 +536,14 @@ By modifying the callback parameter the sorting order can be influenced: ```js //ascending order async.sortBy([1,9,3,5], function(x, callback){ - callback(err, x); + callback(null, x); }, function(err,result){ //result callback } ); //descending order async.sortBy([1,9,3,5], function(x, callback){ - callback(err, x*-1); //<- x*-1 instead of x, turns the order around + callback(null, x*-1); //<- x*-1 instead of x, turns the order around }, function(err,result){ //result callback } ); From 2bbfc0ce963f82038c9a2aec2709dc2db67cb6c9 Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Fri, 21 Nov 2014 15:15:23 -0800 Subject: [PATCH 028/956] Further clarification, link compose --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9ae16b27d..8388c5d3f 100644 --- a/README.md +++ b/README.md @@ -972,8 +972,8 @@ add1mul3(4, function (err, result) { ### seq(fn1, fn2...) Version of the compose function that is more natural to read. -Each following function consumes the return value of the former function. -It is the equivalent of the compose function with reversed arguments. +Each function consumes the return value of the previous function. +It is the equivalent of [`compose`](#compose) with the arguments reversed. Each function is executed with the `this` binding of the composed function. From 191e569e1e66829d4d38fd632c18a75cad8c7a41 Mon Sep 17 00:00:00 2001 From: Fritz Lin Date: Wed, 28 May 2014 23:00:43 +0800 Subject: [PATCH 029/956] Update README.md remove excess `}` --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 8388c5d3f..be48cea70 100644 --- a/README.md +++ b/README.md @@ -1004,7 +1004,6 @@ app.get('/cats', function(request, response) { response.json({ status: 'ok', message: 'Cats found', data: cats }); } }); -} }); ``` From 63d4e88857c7793857a72b6ce5e18512b5175ead Mon Sep 17 00:00:00 2001 From: Joanna Trela Date: Sat, 24 May 2014 15:55:22 +0200 Subject: [PATCH 030/956] #524 async.queue documentation typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index be48cea70..279cde9f7 100644 --- a/README.md +++ b/README.md @@ -1115,7 +1115,7 @@ q.push({name: 'bar'}, function (err) { // add some items to the queue (batch-wise) q.push([{name: 'baz'},{name: 'bay'},{name: 'bax'}], function (err) { - console.log('finished processing bar'); + console.log('finished processing item'); }); // add some items to the front of the queue From a3a77e1fb62be36bbdc8356f9f2ea0d60e79dc9c Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Sat, 29 Nov 2014 15:54:00 -0800 Subject: [PATCH 031/956] Remove errant debugger statements, fixes #563 --- test/test-async.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/test-async.js b/test/test-async.js index 6a3560694..50690fd0e 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -1996,7 +1996,6 @@ exports['doUntil'] = function (test) { var count = 0; async.doUntil( function (cb) { - debugger call_order.push(['iterator', count]); count++; cb(); @@ -2024,7 +2023,6 @@ exports['doUntil callback params'] = function (test) { var count = 0; async.doUntil( function (cb) { - debugger call_order.push(['iterator', count]); count++; cb(null, count); @@ -2091,7 +2089,6 @@ exports['doWhilst'] = function (test) { return (count < 5); }, function (err) { - debugger test.same(call_order, [ ['iterator', 0], ['test', 1], ['iterator', 1], ['test', 2], @@ -2120,7 +2117,6 @@ exports['doWhilst callback params'] = function (test) { return (c < 5); }, function (err) { - debugger test.same(call_order, [ ['iterator', 0], ['test', 1], ['iterator', 1], ['test', 2], From 33fe035af9a2216b6db29221ce27437b9917397f Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Sat, 29 Nov 2014 16:01:48 -0800 Subject: [PATCH 032/956] Document drain removal from kill(), fixes #550 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 279cde9f7..69729c0f7 100644 --- a/README.md +++ b/README.md @@ -1085,7 +1085,7 @@ methods: * `paused` - a boolean for determining whether the queue is in a paused state * `pause()` - a function that pauses the processing of tasks until `resume()` is called. * `resume()` - a function that resumes the processing of queued tasks when the queue is paused. -* `kill()` - a function that empties remaining tasks from the queue forcing it to go idle. +* `kill()` - a function that removes the `drain` callback and empties remaining tasks from the queue forcing it to go idle. __Example__ From f345f97237e3c6cf6a760e8ff6b7ecc3e36e6397 Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Sun, 30 Nov 2014 14:43:21 -0800 Subject: [PATCH 033/956] Fix incorrect test & favor Date valueOf for old IE Fixes #666. --- test/test-async.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/test/test-async.js b/test/test-async.js index 50690fd0e..7529f1ff9 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -2455,8 +2455,10 @@ exports['queue pause'] = function(test) { tasks = [ 1, 2, 3, 4, 5, 6 ], elapsed = (function () { - var start = +Date.now(); - return function () { return Math.floor((+Date.now() - start) / 100) * 100; }; + var start = (new Date()).valueOf(); + return function () { + return Math.round(((new Date()).valueOf() - start) / 100) * 100; + }; })(); var q = async.queue(function (task, callback) { @@ -2506,8 +2508,10 @@ exports['queue pause with concurrency'] = function(test) { tasks = [ 1, 2, 3, 4, 5, 6 ], elapsed = (function () { - var start = +Date.now(); - return function () { return Math.floor((+Date.now() - start) / 100) * 100; }; + var start = (new Date()).valueOf(); + return function () { + return Math.round(((new Date()).valueOf() - start) / 100) * 100; + }; })(); var q = async.queue(function (task, callback) { From 6c9d9c3af98dd9811f96d67dd4c26a94467da77b Mon Sep 17 00:00:00 2001 From: jsdevel Date: Thu, 3 Jul 2014 05:05:45 -0700 Subject: [PATCH 034/956] chmod -x on lib/async.js --- lib/async.js | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 lib/async.js diff --git a/lib/async.js b/lib/async.js old mode 100755 new mode 100644 From 62e24b388687f5b93737b74138c5db10c3be6922 Mon Sep 17 00:00:00 2001 From: Qix Date: Mon, 15 Sep 2014 18:02:50 -0600 Subject: [PATCH 035/956] improved seq() documentation wording Two changes were made here: - `latter` was changed to `former` (that caused a good deal of headache the other day) - added some clarification as to what exactly differs from the `compose()` function Since it's not entirely clear that `compose()` doesn't work left to right, `seq()` should better indicate that it does. --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8f4e98c03..55cea7340 100644 --- a/README.md +++ b/README.md @@ -972,7 +972,8 @@ add1mul3(4, function (err, result) { ### seq(fn1, fn2...) Version of the compose function that is more natural to read. -Each following function consumes the return value of the latter function. +Each following function consumes the return value of the former function. +It is the equivalent of the compose function with reversed arguments. Each function is executed with the `this` binding of the composed function. From c3b6ee677724c933562eaf2ebf2e9878e4bff25e Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Mon, 13 Oct 2014 10:53:26 -0700 Subject: [PATCH 036/956] Improve documentation for async.seq - seq requires the last argument to be a function - seq short-circuits on errors which means using handleError is redundant --- README.md | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 55cea7340..550eb20be 100644 --- a/README.md +++ b/README.md @@ -990,28 +990,21 @@ __Example__ // This example uses `seq` function to avoid overnesting and error // handling clutter. app.get('/cats', function(request, response) { - function handleError(err, data, callback) { - if (err) { - console.error(err); - response.json({ status: 'error', message: err.message }); - } - else { - callback(data); - } - } var User = request.models.User; async.seq( _.bind(User.get, User), // 'User.get' has signature (id, callback(err, data)) - handleError, function(user, fn) { user.getCats(fn); // 'getCats' has signature (callback(err, data)) - }, - handleError, - function(cats) { + } + )(req.session.user_id, function (err, cats) { + if (err) { + console.error(err); + response.json({ status: 'error', message: err.message }); + } else { response.json({ status: 'ok', message: 'Cats found', data: cats }); } - )(req.session.user_id); - } + }); +} }); ``` From b2a16b0bd3592f24a63620ad03cccf7908cc7ff8 Mon Sep 17 00:00:00 2001 From: Anup Bishnoi Date: Tue, 28 Oct 2014 20:15:13 -0400 Subject: [PATCH 037/956] Fix incorrect sample code --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 550eb20be..9ae16b27d 100644 --- a/README.md +++ b/README.md @@ -536,14 +536,14 @@ By modifying the callback parameter the sorting order can be influenced: ```js //ascending order async.sortBy([1,9,3,5], function(x, callback){ - callback(err, x); + callback(null, x); }, function(err,result){ //result callback } ); //descending order async.sortBy([1,9,3,5], function(x, callback){ - callback(err, x*-1); //<- x*-1 instead of x, turns the order around + callback(null, x*-1); //<- x*-1 instead of x, turns the order around }, function(err,result){ //result callback } ); From 1b0072ec2dee302ced32dd76332f514f3994a6a9 Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Fri, 21 Nov 2014 15:15:23 -0800 Subject: [PATCH 038/956] Further clarification, link compose --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9ae16b27d..8388c5d3f 100644 --- a/README.md +++ b/README.md @@ -972,8 +972,8 @@ add1mul3(4, function (err, result) { ### seq(fn1, fn2...) Version of the compose function that is more natural to read. -Each following function consumes the return value of the former function. -It is the equivalent of the compose function with reversed arguments. +Each function consumes the return value of the previous function. +It is the equivalent of [`compose`](#compose) with the arguments reversed. Each function is executed with the `this` binding of the composed function. From 0b45fac0f4e66979940ad5a01e5aac70717edcbe Mon Sep 17 00:00:00 2001 From: Fritz Lin Date: Wed, 28 May 2014 23:00:43 +0800 Subject: [PATCH 039/956] Update README.md remove excess `}` --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 8388c5d3f..be48cea70 100644 --- a/README.md +++ b/README.md @@ -1004,7 +1004,6 @@ app.get('/cats', function(request, response) { response.json({ status: 'ok', message: 'Cats found', data: cats }); } }); -} }); ``` From caa24adb1a2cdabbcc8b77cf5f6ce464931ceda6 Mon Sep 17 00:00:00 2001 From: Joanna Trela Date: Sat, 24 May 2014 15:55:22 +0200 Subject: [PATCH 040/956] #524 async.queue documentation typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index be48cea70..279cde9f7 100644 --- a/README.md +++ b/README.md @@ -1115,7 +1115,7 @@ q.push({name: 'bar'}, function (err) { // add some items to the queue (batch-wise) q.push([{name: 'baz'},{name: 'bay'},{name: 'bax'}], function (err) { - console.log('finished processing bar'); + console.log('finished processing item'); }); // add some items to the front of the queue From 54d2da53b76aa1a181777761acb14da9a80f9b49 Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Sat, 29 Nov 2014 15:54:00 -0800 Subject: [PATCH 041/956] Remove errant debugger statements, fixes #563 --- test/test-async.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/test/test-async.js b/test/test-async.js index 6a3560694..50690fd0e 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -1996,7 +1996,6 @@ exports['doUntil'] = function (test) { var count = 0; async.doUntil( function (cb) { - debugger call_order.push(['iterator', count]); count++; cb(); @@ -2024,7 +2023,6 @@ exports['doUntil callback params'] = function (test) { var count = 0; async.doUntil( function (cb) { - debugger call_order.push(['iterator', count]); count++; cb(null, count); @@ -2091,7 +2089,6 @@ exports['doWhilst'] = function (test) { return (count < 5); }, function (err) { - debugger test.same(call_order, [ ['iterator', 0], ['test', 1], ['iterator', 1], ['test', 2], @@ -2120,7 +2117,6 @@ exports['doWhilst callback params'] = function (test) { return (c < 5); }, function (err) { - debugger test.same(call_order, [ ['iterator', 0], ['test', 1], ['iterator', 1], ['test', 2], From 788143be2cbaec900da5529c14773be8cd91f717 Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Sat, 29 Nov 2014 16:01:48 -0800 Subject: [PATCH 042/956] Document drain removal from kill(), fixes #550 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 279cde9f7..69729c0f7 100644 --- a/README.md +++ b/README.md @@ -1085,7 +1085,7 @@ methods: * `paused` - a boolean for determining whether the queue is in a paused state * `pause()` - a function that pauses the processing of tasks until `resume()` is called. * `resume()` - a function that resumes the processing of queued tasks when the queue is paused. -* `kill()` - a function that empties remaining tasks from the queue forcing it to go idle. +* `kill()` - a function that removes the `drain` callback and empties remaining tasks from the queue forcing it to go idle. __Example__ From 5fe4e2024b784a28ddbe5e39ee9426e30359591c Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Sun, 30 Nov 2014 14:43:21 -0800 Subject: [PATCH 043/956] Fix incorrect test & favor Date valueOf for old IE Fixes #666. --- test/test-async.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/test/test-async.js b/test/test-async.js index 50690fd0e..7529f1ff9 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -2455,8 +2455,10 @@ exports['queue pause'] = function(test) { tasks = [ 1, 2, 3, 4, 5, 6 ], elapsed = (function () { - var start = +Date.now(); - return function () { return Math.floor((+Date.now() - start) / 100) * 100; }; + var start = (new Date()).valueOf(); + return function () { + return Math.round(((new Date()).valueOf() - start) / 100) * 100; + }; })(); var q = async.queue(function (task, callback) { @@ -2506,8 +2508,10 @@ exports['queue pause with concurrency'] = function(test) { tasks = [ 1, 2, 3, 4, 5, 6 ], elapsed = (function () { - var start = +Date.now(); - return function () { return Math.floor((+Date.now() - start) / 100) * 100; }; + var start = (new Date()).valueOf(); + return function () { + return Math.round(((new Date()).valueOf() - start) / 100) * 100; + }; })(); var q = async.queue(function (task, callback) { From cc75bec43db06619d9a556a9484830e9412655e9 Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Thu, 18 Dec 2014 12:12:03 -0800 Subject: [PATCH 044/956] =?UTF-8?q?licenses=20=E2=86=92=20license?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 09f76aac4..db6ed4725 100644 --- a/package.json +++ b/package.json @@ -11,12 +11,10 @@ "bugs" : { "url" : "https://github.com/caolan/async/issues" }, - "licenses" : [ - { - "type" : "MIT", - "url" : "https://github.com/caolan/async/raw/master/LICENSE" - } - ], + "license" : { + "type" : "MIT", + "url" : "https://github.com/caolan/async/raw/master/LICENSE" + }, "devDependencies": { "nodeunit": ">0.0.0", "uglify-js": "1.2.x", From b6517e28e66d65a54e478a8ebb63340ea3c11600 Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Thu, 18 Dec 2014 12:12:27 -0800 Subject: [PATCH 045/956] Add lodash to devDependencies --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index db6ed4725..1d1687019 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,8 @@ "devDependencies": { "nodeunit": ">0.0.0", "uglify-js": "1.2.x", - "nodelint": ">0.0.0" + "nodelint": ">0.0.0", + "lodash": ">=2.4.1" }, "jam": { "main": "lib/async.js", From b52a1c61e67acecda7e4e1e435a48df7759c412b Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Thu, 18 Dec 2014 12:16:33 -0800 Subject: [PATCH 046/956] Add keywords --- package.json | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/package.json b/package.json index 1d1687019..de1df31bb 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,12 @@ "main": "./lib/async", "author": "Caolan McMahon", "version": "0.9.0", + "keywords": [ + "async", + "callback", + "utility", + "module" + ], "repository" : { "type" : "git", "url" : "https://github.com/caolan/async.git" From 2de4bf8d3e19e14c874d6ce0ba2e24c6c5810dc8 Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Thu, 18 Dec 2014 12:29:32 -0800 Subject: [PATCH 047/956] Add script to sync package manager info --- support/sync-package-managers.js | 55 ++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100755 support/sync-package-managers.js diff --git a/support/sync-package-managers.js b/support/sync-package-managers.js new file mode 100755 index 000000000..3b44fcbc4 --- /dev/null +++ b/support/sync-package-managers.js @@ -0,0 +1,55 @@ +#!/usr/bin/env node + +// This should probably be its own module but complaints about bower/etc. +// support keep coming up and I'd rather just enable the workflow here for now +// and figure out where this should live later. -- @beaugunderson + +var fs = require('fs'); +var _ = require('lodash'); + +var packageJson = require('../package.json'); + +var IGNORES = ['**/.*', 'node_modules', 'bower_components', 'test', 'tests']; +var INCLUDES = ['lib/async.js', 'README.md', 'LICENSE']; +var REPOSITORY_NAME = 'caolan/async'; + +var LICENSE_NAME = packageJson.license.type; + +packageJson.jam = { + main: packageJson.main, + include: INCLUDES +}; + +packageJson.spm = { + main: packageJson.main +}; + +packageJson.volo = { + main: packageJson.main, + ignore: IGNORES +}; + +var bowerSpecific = { + moduleType: ['amd', 'globals', 'node'], + license: LICENSE_NAME, + ignore: IGNORES, + authors: [packageJson.author] +}; + +var bowerInclude = ['name', 'description', 'version', 'main', 'keywords', + 'homepage', 'repository', 'devDependencies']; + +var componentSpecific = { + license: LICENSE_NAME, + repository: REPOSITORY_NAME, + scripts: [packageJson.main] +}; + +var componentInclude = ['name', 'description', 'version', 'keywords']; + +var bowerJson = _.merge({}, _.pick(packageJson, bowerInclude), bowerSpecific); +var componentJson = _.merge({}, _.pick(packageJson, componentInclude), componentSpecific); + +fs.writeFileSync('./bower.json', JSON.stringify(bowerJson, null, 2)); +fs.writeFileSync('./component.json', JSON.stringify(componentJson, null, 2)); +fs.writeFileSync('./package.json', JSON.stringify(packageJson, null, 2)); From bb53b1a488b6d67a8b93dab2d1718197ed992cbc Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Thu, 18 Dec 2014 12:30:16 -0800 Subject: [PATCH 048/956] Update package manager info Fixes #544 Fixes #565 Fixes #660 Closes #653 Closes #677 --- bower.json | 38 +++++++++++++++++++++ component.json | 21 +++++++----- package.json | 93 ++++++++++++++++++++++++++++---------------------- 3 files changed, 104 insertions(+), 48 deletions(-) create mode 100644 bower.json diff --git a/bower.json b/bower.json new file mode 100644 index 000000000..0ab1093ee --- /dev/null +++ b/bower.json @@ -0,0 +1,38 @@ +{ + "name": "async", + "description": "Higher-order functions and common patterns for asynchronous code", + "version": "0.9.0", + "main": "lib/async.js", + "keywords": [ + "async", + "callback", + "utility", + "module" + ], + "repository": { + "type": "git", + "url": "https://github.com/caolan/async.git" + }, + "devDependencies": { + "nodeunit": ">0.0.0", + "uglify-js": "1.2.x", + "nodelint": ">0.0.0", + "lodash": ">=2.4.1" + }, + "moduleType": [ + "amd", + "globals", + "node" + ], + "license": "MIT", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ], + "authors": [ + "Caolan McMahon" + ] +} \ No newline at end of file diff --git a/component.json b/component.json index bbb011548..6cf0059de 100644 --- a/component.json +++ b/component.json @@ -1,11 +1,16 @@ { "name": "async", - "repo": "caolan/async", "description": "Higher-order functions and common patterns for asynchronous code", - "version": "0.1.23", - "keywords": [], - "dependencies": {}, - "development": {}, - "main": "lib/async.js", - "scripts": [ "lib/async.js" ] -} + "version": "0.9.0", + "keywords": [ + "async", + "callback", + "utility", + "module" + ], + "license": "MIT", + "repository": "caolan/async", + "scripts": [ + "lib/async.js" + ] +} \ No newline at end of file diff --git a/package.json b/package.json index de1df31bb..03ec40a95 100644 --- a/package.json +++ b/package.json @@ -1,41 +1,54 @@ { - "name": "async", - "description": "Higher-order functions and common patterns for asynchronous code", - "main": "./lib/async", - "author": "Caolan McMahon", - "version": "0.9.0", - "keywords": [ - "async", - "callback", - "utility", - "module" - ], - "repository" : { - "type" : "git", - "url" : "https://github.com/caolan/async.git" - }, - "bugs" : { - "url" : "https://github.com/caolan/async/issues" - }, - "license" : { - "type" : "MIT", - "url" : "https://github.com/caolan/async/raw/master/LICENSE" - }, - "devDependencies": { - "nodeunit": ">0.0.0", - "uglify-js": "1.2.x", - "nodelint": ">0.0.0", - "lodash": ">=2.4.1" - }, - "jam": { - "main": "lib/async.js", - "include": [ - "lib/async.js", - "README.md", - "LICENSE" - ] - }, - "scripts": { - "test": "nodeunit test/test-async.js" - } -} + "name": "async", + "description": "Higher-order functions and common patterns for asynchronous code", + "main": "lib/async.js", + "author": "Caolan McMahon", + "version": "0.9.0", + "keywords": [ + "async", + "callback", + "utility", + "module" + ], + "repository": { + "type": "git", + "url": "https://github.com/caolan/async.git" + }, + "bugs": { + "url": "https://github.com/caolan/async/issues" + }, + "license": { + "type": "MIT", + "url": "https://github.com/caolan/async/raw/master/LICENSE" + }, + "devDependencies": { + "nodeunit": ">0.0.0", + "uglify-js": "1.2.x", + "nodelint": ">0.0.0", + "lodash": ">=2.4.1" + }, + "jam": { + "main": "lib/async.js", + "include": [ + "lib/async.js", + "README.md", + "LICENSE" + ] + }, + "scripts": { + "test": "nodeunit test/test-async.js" + }, + "spm": { + "main": "lib/async.js" + }, + "volo": { + "main": "lib/async.js", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ] + } +} \ No newline at end of file From ce2ad034e1df5bd20d74434692a4f3ff1b389bda Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Thu, 18 Dec 2014 12:31:59 -0800 Subject: [PATCH 049/956] 0.9.1 --- bower.json | 2 +- component.json | 2 +- package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bower.json b/bower.json index 0ab1093ee..b37c30e36 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "async", "description": "Higher-order functions and common patterns for asynchronous code", - "version": "0.9.0", + "version": "0.9.1", "main": "lib/async.js", "keywords": [ "async", diff --git a/component.json b/component.json index 6cf0059de..97399fe0f 100644 --- a/component.json +++ b/component.json @@ -1,7 +1,7 @@ { "name": "async", "description": "Higher-order functions and common patterns for asynchronous code", - "version": "0.9.0", + "version": "0.9.1", "keywords": [ "async", "callback", diff --git a/package.json b/package.json index 03ec40a95..e279ef168 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "description": "Higher-order functions and common patterns for asynchronous code", "main": "lib/async.js", "author": "Caolan McMahon", - "version": "0.9.0", + "version": "0.9.1", "keywords": [ "async", "callback", From 48cf3910335a75eab393c76de5f5b7954b879cfc Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Thu, 18 Dec 2014 12:48:53 -0800 Subject: [PATCH 050/956] Add categories for jamjs --- package.json | 3 +++ support/sync-package-managers.js | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index e279ef168..f07f67118 100644 --- a/package.json +++ b/package.json @@ -33,6 +33,9 @@ "lib/async.js", "README.md", "LICENSE" + ], + "categories": [ + "Utilities" ] }, "scripts": { diff --git a/support/sync-package-managers.js b/support/sync-package-managers.js index 3b44fcbc4..310bbe6e3 100755 --- a/support/sync-package-managers.js +++ b/support/sync-package-managers.js @@ -17,7 +17,8 @@ var LICENSE_NAME = packageJson.license.type; packageJson.jam = { main: packageJson.main, - include: INCLUDES + include: INCLUDES, + categories: ['Utilities'] }; packageJson.spm = { From 20bd17a9f93251be116e5833d82d2cc022f861ed Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Thu, 18 Dec 2014 12:49:06 -0800 Subject: [PATCH 051/956] List alternate package managers Closes #641 --- README.md | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 69729c0f7..5123e9751 100644 --- a/README.md +++ b/README.md @@ -5,8 +5,16 @@ Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript. Although originally designed for -use with [Node.js](http://nodejs.org), it can also be used directly in the -browser. Also supports [component](https://github.com/component/component). +use with [Node.js](http://nodejs.org) and installable via `npm install async`, +it can also be used directly in the browser. + +Async is also installable via: + +- [bower](http://bower.io/): `bower install async` +- [component](https://github.com/component/component): `component install + caolan/async` +- [jam](http://jamjs.org/): `jam install async` +- [spm](http://spmjs.io/): `spm install async` Async provides around 20 functions that include the usual 'functional' suspects (`map`, `reduce`, `filter`, `each`…) as well as some common patterns From 608486600bdbff4317ac26a74ee96d44d188cb95 Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Thu, 18 Dec 2014 12:52:18 -0800 Subject: [PATCH 052/956] Add node 0.11 --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 6e5919de3..05d299e67 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,3 +1,4 @@ language: node_js node_js: - "0.10" + - "0.11" From baee7a647e77195ee897caf1e17374eae473e517 Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Thu, 18 Dec 2014 12:55:26 -0800 Subject: [PATCH 053/956] 0.9.2 --- bower.json | 2 +- component.json | 2 +- package.json | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/bower.json b/bower.json index b37c30e36..e6f1b42f5 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "async", "description": "Higher-order functions and common patterns for asynchronous code", - "version": "0.9.1", + "version": "0.9.2", "main": "lib/async.js", "keywords": [ "async", diff --git a/component.json b/component.json index 97399fe0f..5003a7c52 100644 --- a/component.json +++ b/component.json @@ -1,7 +1,7 @@ { "name": "async", "description": "Higher-order functions and common patterns for asynchronous code", - "version": "0.9.1", + "version": "0.9.2", "keywords": [ "async", "callback", diff --git a/package.json b/package.json index f07f67118..f5debe2af 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "description": "Higher-order functions and common patterns for asynchronous code", "main": "lib/async.js", "author": "Caolan McMahon", - "version": "0.9.1", + "version": "0.9.2", "keywords": [ "async", "callback", From b0dab0740aff1c7b4ba942f6139fa2286b2dc972 Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Thu, 18 Dec 2014 14:52:40 -0800 Subject: [PATCH 054/956] Update test for node 0.10 and above --- test/test-async.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/test/test-async.js b/test/test-async.js index 7529f1ff9..2d46b8cef 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -1882,15 +1882,13 @@ exports['noConflict - node only'] = function(test){ // node only test test.expect(3); var fs = require('fs'); + var vm = require('vm'); var filename = __dirname + '/../lib/async.js'; fs.readFile(filename, function(err, content){ if(err) return test.done(); - // Script -> NodeScript in node v0.6.x - var Script = process.binding('evals').Script || process.binding('evals').NodeScript; - - var s = new Script(content, filename); - var s2 = new Script( + var s = vm.createScript(content, filename); + var s2 = vm.createScript( content + 'this.async2 = this.async.noConflict();', filename ); From 0789c3185a0d67dca06176d2b4e4aac9373cac12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=B0=D0=BB=D1=8C=D0=BE?= Date: Tue, 30 Dec 2014 16:41:16 +0200 Subject: [PATCH 055/956] Update README.md Update times signature. Tried to be more exact and not confusing the map's callback with this "callback" --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5123e9751..efa80cb86 100644 --- a/README.md +++ b/README.md @@ -1508,7 +1508,8 @@ you would use with [`map`](#map). __Arguments__ * `n` - The number of times to run the function. -* `callback` - The function to call `n` times. +* `iterator` - The function to call `n` times. +* `callback` - see [`map`](#map) __Example__ From fa07047e3eeab6d18bc56d758d6f8cdef83d87bf Mon Sep 17 00:00:00 2001 From: Li Xin Date: Sun, 4 Jan 2015 15:50:29 +0800 Subject: [PATCH 056/956] Delete unnecessary space --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5123e9751..acbfebe85 100644 --- a/README.md +++ b/README.md @@ -210,7 +210,7 @@ async.each(openFiles, saveFile, function(err){ ```js // assuming openFiles is an array of file names -async.each(openFiles, function( file, callback) { +async.each(openFiles, function(file, callback) { // Perform operation on file here. console.log('Processing file ' + file); From ca53d42d9dc80e12d8676b7453d748e78dfe9873 Mon Sep 17 00:00:00 2001 From: wltsmrz Date: Tue, 6 Jan 2015 09:59:08 -0800 Subject: [PATCH 057/956] Document async.setImmediate() --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index acbfebe85..420cd6a4c 100644 --- a/README.md +++ b/README.md @@ -1475,7 +1475,7 @@ three --------------------------------------- -### nextTick(callback) +### nextTick(callback), setImmediate(callback) Calls `callback` on a later loop around the event loop. In Node.js this just calls `process.nextTick`; in the browser it falls back to `setImmediate(callback)` From 6d612c8bceff7a88ebcc263a139f336adec79e9e Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Mon, 19 Jan 2015 23:53:49 -0800 Subject: [PATCH 058/956] adding docs for forEachOf methods --- README.md | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/README.md b/README.md index 420cd6a4c..0f2e41bc3 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,9 @@ Usage: * [`each`](#each) * [`eachSeries`](#eachSeries) * [`eachLimit`](#eachLimit) +* [`forEachOf`](#forEachOf) +* [`forEachOfSeries`](#forEachOfSeries) +* [`forEachOfLimit`](#forEachOfLimit) * [`map`](#map) * [`mapSeries`](#mapSeries) * [`mapLimit`](#mapLimit) @@ -280,6 +283,53 @@ async.eachLimit(documents, 20, requestApi, function(err){ }); ``` +--------------------------------------- + + + +### forEachOf(obj, iterator, callback) + +Like `each`, except that it iterates over objects, and passes the key as the second argument to the iterator. + +__Arguments__ + +* `obj` - An object or array to iterate over. +* `iterator(item, key, callback)` - A function to apply to each item in `obj`. +The `key` is the item's key, or index in the case of an array. The iterator is +passed a `callback(err)` which must be called once it has completed. If no +error has occurred, the callback should be run without arguments or with an +explicit `null` argument. +* `callback(err)` - A callback which is called when all `iterator` functions have finished, or an error occurs. + +__Example__ + +```js +var obj = {a: 1, b: 2, c: 3}; + +async.forEachOf(obj, function (value, key, callback) { + console.log(value + ":" + key) // 1:a, 2:b, 3:c, etc... + callback(); +}, function (err) { + +}) +``` + +--------------------------------------- + + + +### forEachOfSeries(obj, iterator, callback) + +Like [`forEachOf`](#forEachOf), except only one `iterator` is run at a time. The order of execution is not guaranteed for objects, but it will be for arrays. +--------------------------------------- + + + +### forEachOfLimit(obj, limit, iterator, callback) + +Like [`forEachOf`](#forEachOf), except only one the number of `iterator`s running at a time is controlled by `limit`. The order of execution is not guaranteed for objects, but it will be for arrays. + + --------------------------------------- From 52dd10d744a3c2d82b3f29466462e569adc8de39 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Tue, 20 Jan 2015 11:18:50 -0800 Subject: [PATCH 059/956] reordered functions, added aliases, simplified eachOfLimit logic --- lib/async.js | 104 +++++++++++++++++++++++---------------------- test/test-async.js | 97 +++++++++++++++++++++--------------------- 2 files changed, 102 insertions(+), 99 deletions(-) diff --git a/lib/async.js b/lib/async.js index c99024be4..185812fd9 100644 --- a/lib/async.js +++ b/lib/async.js @@ -171,7 +171,56 @@ }; async.forEachSeries = async.eachSeries; - async.forEachOf = function (object, iterator, callback) { + + async.eachLimit = function (arr, limit, iterator, callback) { + var fn = _eachLimit(limit); + fn.apply(null, [arr, iterator, callback]); + }; + async.forEachLimit = async.eachLimit; + + var _eachLimit = function (limit) { + + return function (arr, iterator, callback) { + callback = callback || function () {}; + if (!arr.length || limit <= 0) { + return callback(); + } + var completed = 0; + var started = 0; + var running = 0; + + (function replenish () { + if (completed >= arr.length) { + return callback(); + } + + while (running < limit && started < arr.length) { + started += 1; + running += 1; + iterator(arr[started - 1], function (err) { + if (err) { + callback(err); + callback = function () {}; + } + else { + completed += 1; + running -= 1; + if (completed >= arr.length) { + callback(); + } + else { + replenish(); + } + } + }); + } + })(); + }; + }; + + + + async.forEachOf = async.eachOf = function (object, iterator, callback) { callback = callback || function () {}; var size = object.length || _keys(object).length; var completed = 0 @@ -193,7 +242,7 @@ }); }; - async.forEachOfSeries = function (obj, iterator, callback) { + async.forEachOfSeries = async.eachOfSeries = function (obj, iterator, callback) { callback = callback || function () {}; var keys = _keys(obj); var size = keys.length; @@ -230,56 +279,9 @@ }; - async.eachLimit = function (arr, limit, iterator, callback) { - var fn = _eachLimit(limit); - fn.apply(null, [arr, iterator, callback]); - }; - async.forEachLimit = async.eachLimit; - - var _eachLimit = function (limit) { - - return function (arr, iterator, callback) { - callback = callback || function () {}; - if (!arr.length || limit <= 0) { - return callback(); - } - var completed = 0; - var started = 0; - var running = 0; - - (function replenish () { - if (completed >= arr.length) { - return callback(); - } - - while (running < limit && started < arr.length) { - started += 1; - running += 1; - iterator(arr[started - 1], function (err) { - if (err) { - callback(err); - callback = function () {}; - } - else { - completed += 1; - running -= 1; - if (completed >= arr.length) { - callback(); - } - else { - replenish(); - } - } - }); - } - })(); - }; - }; - - async.forEachOfLimit = function (obj, limit, iterator, callback) { - var fn = obj.constructor === Array ? _forEachOfLimit(limit) : _forEachOfLimit(limit); - fn.apply(null, [obj, iterator, callback]); + async.forEachOfLimit = async.eachOfLimit = function (obj, limit, iterator, callback) { + _forEachOfLimit(limit)(obj, iterator, callback); }; var _forEachOfLimit = function (limit) { diff --git a/test/test-async.js b/test/test-async.js index f5fc72ec3..73e712214 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -1256,54 +1256,6 @@ exports['eachSeries no callback'] = function(test){ async.eachSeries([1], eachNoCallbackIterator.bind(this, test)); }; -exports['forEachSeries alias'] = function (test) { - test.strictEqual(async.eachSeries, async.forEachSeries); - test.done(); -}; - -exports['forEachOfSeries'] = function(test){ - var args = []; - async.forEachOfSeries({ a: 1, b: 2 }, forEachOfIterator.bind(this, args), function(err){ - test.same(args, [ "a", 1, "b", 2 ]); - test.done(); - }); -}; - -exports['forEachOfSeries empty object'] = function(test){ - test.expect(1); - async.forEachOfSeries({}, function(x, callback){ - test.ok(false, 'iterator should not be called'); - callback(); - }, function(err){ - test.ok(true, 'should call callback'); - }); - setTimeout(test.done, 25); -}; - -exports['forEachOfSeries error'] = function(test){ - test.expect(2); - var call_order = []; - async.forEachOfSeries({ a: 1, b: 2 }, function(value, key, callback){ - call_order.push(value, key); - callback('error'); - }, function(err){ - test.same(call_order, [ 1, "a" ]); - test.equals(err, 'error'); - }); - setTimeout(test.done, 50); -}; - -exports['forEachOfSeries no callback'] = function(test){ - async.forEachOfSeries({ a: 1 }, forEachOfNoCallbackIterator.bind(this, test)); -}; - -exports['forEachOfSeries with array'] = function(test){ - var args = []; - async.forEachOfSeries([ "a", "b" ], forEachOfIterator.bind(this, args), function(err){ - test.same(args, [ 0, "a", 1, "b" ]); - test.done(); - }); -}; exports['eachLimit'] = function(test){ var args = []; @@ -1392,6 +1344,55 @@ exports['eachLimit synchronous'] = function(test){ }); }; +exports['forEachSeries alias'] = function (test) { + test.strictEqual(async.eachSeries, async.forEachSeries); + test.done(); +}; + +exports['forEachOfSeries'] = function(test){ + var args = []; + async.forEachOfSeries({ a: 1, b: 2 }, forEachOfIterator.bind(this, args), function(err){ + test.same(args, [ "a", 1, "b", 2 ]); + test.done(); + }); +}; + +exports['forEachOfSeries empty object'] = function(test){ + test.expect(1); + async.forEachOfSeries({}, function(x, callback){ + test.ok(false, 'iterator should not be called'); + callback(); + }, function(err){ + test.ok(true, 'should call callback'); + }); + setTimeout(test.done, 25); +}; + +exports['forEachOfSeries error'] = function(test){ + test.expect(2); + var call_order = []; + async.forEachOfSeries({ a: 1, b: 2 }, function(value, key, callback){ + call_order.push(value, key); + callback('error'); + }, function(err){ + test.same(call_order, [ 1, "a" ]); + test.equals(err, 'error'); + }); + setTimeout(test.done, 50); +}; + +exports['forEachOfSeries no callback'] = function(test){ + async.forEachOfSeries({ a: 1 }, forEachOfNoCallbackIterator.bind(this, test)); +}; + +exports['forEachOfSeries with array'] = function(test){ + var args = []; + async.forEachOfSeries([ "a", "b" ], forEachOfIterator.bind(this, args), function(err){ + test.same(args, [ 0, "a", 1, "b" ]); + test.done(); + }); +}; + exports['forEachLimit alias'] = function (test) { test.strictEqual(async.eachLimit, async.forEachLimit); test.done(); From ae5d25f9e139af86bc5ef1431380e6f20f8eeea9 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Tue, 20 Jan 2015 11:30:12 -0800 Subject: [PATCH 060/956] tightening up the documentation --- README.md | 29 ++++++++++++++++++++++------- lib/async.js | 2 +- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 0f2e41bc3..887e386eb 100644 --- a/README.md +++ b/README.md @@ -194,7 +194,8 @@ __Arguments__ * `iterator(item, callback)` - A function to apply to each item in `arr`. The iterator is passed a `callback(err)` which must be called once it has completed. If no error has occurred, the `callback` should be run without - arguments or with an explicit `null` argument. + arguments or with an explicit `null` argument. The array index is not passed + to the iterator. If you need the index, use [`forEachOf`](#forEachOf). * `callback(err)` - A callback which is called when all `iterator` functions have finished, or an error occurs. @@ -286,6 +287,7 @@ async.eachLimit(documents, 20, requestApi, function(err){ --------------------------------------- + ### forEachOf(obj, iterator, callback) @@ -304,30 +306,43 @@ explicit `null` argument. __Example__ ```js -var obj = {a: 1, b: 2, c: 3}; +var obj = {dev: "/dev.json", test: "/test.json", prod: "/prod.json"}; +var configs = {}; async.forEachOf(obj, function (value, key, callback) { - console.log(value + ":" + key) // 1:a, 2:b, 3:c, etc... - callback(); + fs.readFile(__dirname + value, "utf8", function (err, data) { + if (err) return callback(err); + try { + configs[key] = JSON.parse(data); + } catch (e) { + return callback(e); + } + callback(); + }) }, function (err) { - + if (err) console.error(err.message); + // configs is now a map of JSON data + doSomethingWith(configs); }) ``` --------------------------------------- + ### forEachOfSeries(obj, iterator, callback) -Like [`forEachOf`](#forEachOf), except only one `iterator` is run at a time. The order of execution is not guaranteed for objects, but it will be for arrays. +Like [`forEachOf`](#forEachOf), except only one `iterator` is run at a time. The order of execution is not guaranteed for objects, but it will be guaranteed for arrays. + --------------------------------------- + ### forEachOfLimit(obj, limit, iterator, callback) -Like [`forEachOf`](#forEachOf), except only one the number of `iterator`s running at a time is controlled by `limit`. The order of execution is not guaranteed for objects, but it will be for arrays. +Like [`forEachOf`](#forEachOf), except the number of `iterator`s running at a given time is controlled by `limit`. --------------------------------------- diff --git a/lib/async.js b/lib/async.js index 185812fd9..87ebe47a2 100644 --- a/lib/async.js +++ b/lib/async.js @@ -280,7 +280,7 @@ - async.forEachOfLimit = async.eachOfLimit = function (obj, limit, iterator, callback) { + async.forEachOfLimit = async.eachOfLimit = function (obj, limit, iterator, callback) { _forEachOfLimit(limit)(obj, iterator, callback); }; From 2b979cdc2e386ac3ef9fb44aa568f75c43a91fc9 Mon Sep 17 00:00:00 2001 From: eternal student Date: Wed, 4 Feb 2015 11:23:40 +0200 Subject: [PATCH 061/956] Update README.md indentation off by 1 char --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 420cd6a4c..2977c713a 100644 --- a/README.md +++ b/README.md @@ -933,7 +933,7 @@ async.waterfall([ callback(null, 'done'); } ], function (err, result) { - // result now equals 'done' + // result now equals 'done' }); ``` From 2fabc63c2e18892bcebe3f4e7735705eda4fa45d Mon Sep 17 00:00:00 2001 From: eternal student Date: Wed, 4 Feb 2015 13:53:14 +0200 Subject: [PATCH 062/956] Update README.md fix syntax to match rest of examples --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 420cd6a4c..869885292 100644 --- a/README.md +++ b/README.md @@ -921,14 +921,14 @@ __Example__ ```js async.waterfall([ - function(callback){ + function(callback) { callback(null, 'one', 'two'); }, - function(arg1, arg2, callback){ + function(arg1, arg2, callback) { // arg1 now equals 'one' and arg2 now equals 'two' callback(null, 'three'); }, - function(arg1, callback){ + function(arg1, callback) { // arg1 now equals 'three' callback(null, 'done'); } From d00bfc5655d17117900ed17704b1ce0295ce6f2b Mon Sep 17 00:00:00 2001 From: Neamar Date: Mon, 9 Jun 2014 19:56:27 +0200 Subject: [PATCH 063/956] noop --- lib/async.js | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/lib/async.js b/lib/async.js index 394c41cad..8aec08f9c 100644 --- a/lib/async.js +++ b/lib/async.js @@ -10,6 +10,7 @@ (function () { var async = {}; + var noop = function () {}; // global on the server, window in the browser var root, previous_async; @@ -113,7 +114,7 @@ } async.each = function (arr, iterator, callback) { - callback = callback || function () {}; + callback = callback || noop; if (!arr.length) { return callback(); } @@ -124,7 +125,7 @@ function done(err) { if (err) { callback(err); - callback = function () {}; + callback = noop; } else { completed += 1; @@ -137,7 +138,7 @@ async.forEach = async.each; async.eachSeries = function (arr, iterator, callback) { - callback = callback || function () {}; + callback = callback || noop; if (!arr.length) { return callback(); } @@ -146,7 +147,7 @@ iterator(arr[completed], function (err) { if (err) { callback(err); - callback = function () {}; + callback = noop; } else { completed += 1; @@ -172,7 +173,7 @@ var _eachLimit = function (limit) { return function (arr, iterator, callback) { - callback = callback || function () {}; + callback = callback || noop; if (!arr.length || limit <= 0) { return callback(); } @@ -191,7 +192,7 @@ iterator(arr[started - 1], function (err) { if (err) { callback(err); - callback = function () {}; + callback = noop; } else { completed += 1; @@ -342,7 +343,7 @@ iterator(x, function (result) { if (result) { main_callback(x); - main_callback = function () {}; + main_callback = noop; } else { callback(); @@ -360,7 +361,7 @@ iterator(x, function (v) { if (v) { main_callback(true); - main_callback = function () {}; + main_callback = noop; } callback(); }); @@ -376,7 +377,7 @@ iterator(x, function (v) { if (!v) { main_callback(false); - main_callback = function () {}; + main_callback = noop; } callback(); }); @@ -414,7 +415,7 @@ }; async.auto = function (tasks, callback) { - callback = callback || function () {}; + callback = callback || noop; var keys = _keys(tasks); var remainingTasks = keys.length if (!remainingTasks) { @@ -446,7 +447,7 @@ if (!remainingTasks) { var theCallback = callback; // prevent final callback from calling itself if it errors - callback = function () {}; + callback = noop; theCallback(null, results); } @@ -467,7 +468,7 @@ safeResults[k] = args; callback(err, safeResults); // stop subsequent errors hitting callback multiple times - callback = function () {}; + callback = noop; } else { results[k] = args; @@ -527,7 +528,7 @@ }; async.waterfall = function (tasks, callback) { - callback = callback || function () {}; + callback = callback || noop; if (!_isArray(tasks)) { var err = new Error('First argument to waterfall must be an array of functions'); return callback(err); @@ -539,7 +540,7 @@ return function (err) { if (err) { callback.apply(null, arguments); - callback = function () {}; + callback = noop; } else { var args = Array.prototype.slice.call(arguments, 1); @@ -560,7 +561,7 @@ }; var _parallel = function(eachfn, tasks, callback) { - callback = callback || function () {}; + callback = callback || noop; if (_isArray(tasks)) { eachfn.map(tasks, function (fn, callback) { if (fn) { @@ -600,7 +601,7 @@ }; async.series = function (tasks, callback) { - callback = callback || function () {}; + callback = callback || noop; if (_isArray(tasks)) { async.mapSeries(tasks, function (fn, callback) { if (fn) { From ddcee3e0ad11771360a726c460a3427c9c78a566 Mon Sep 17 00:00:00 2001 From: Joseph Orbegoso Pea Date: Wed, 7 May 2014 12:37:56 -0700 Subject: [PATCH 064/956] Better support for browsers. In a RequireJS environment `this` doesn't refer to the `window` object, so async doesn't work there. --- lib/async.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/async.js b/lib/async.js index 394c41cad..f925fc502 100644 --- a/lib/async.js +++ b/lib/async.js @@ -14,7 +14,16 @@ // global on the server, window in the browser var root, previous_async; - root = this; + if (typeof window == 'object' && this === window) { + root = window; + } + else if (typeof global == 'object' && this === global) { + root = global; + } + else { + root = this; + } + if (root != null) { previous_async = root.async; } From 28f6d2b11f4a2ce8d3dfc13baba74279dcde0920 Mon Sep 17 00:00:00 2001 From: Wendy Leung Date: Sun, 15 Feb 2015 12:02:54 -0800 Subject: [PATCH 065/956] Update README.md Fix typos for words: successful, attempts. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 29132719b..6cfb922c5 100644 --- a/README.md +++ b/README.md @@ -1350,7 +1350,7 @@ new tasks much easier (and the code more readable). Attempts to get a successful response from `task` no more than `times` times before returning an error. If the task is successful, the `callback` will be passed the result -of the successfull task. If all attemps fail, the callback will be passed the error and +of the successful task. If all attempts fail, the callback will be passed the error and result (if any) of the final attempt. __Arguments__ From d0149c9978c5903254f1ee115e653d7026fa4c76 Mon Sep 17 00:00:00 2001 From: Elvin Yung Date: Fri, 27 Feb 2015 14:48:37 -0500 Subject: [PATCH 066/956] Wrap makefile cwd strings in quotes --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index bad647c63..4b0f23cbc 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ PACKAGE = asyncjs NODEJS = $(if $(shell test -f /usr/bin/nodejs && echo "true"),nodejs,node) -CWD := $(shell pwd) +CWD := "$(shell pwd)" NODEUNIT = $(CWD)/node_modules/nodeunit/bin/nodeunit UGLIFY = $(CWD)/node_modules/uglify-js/bin/uglifyjs NODELINT = $(CWD)/node_modules/nodelint/nodelint From e7ab7d73d1e81a6965343db09f1e297af9360811 Mon Sep 17 00:00:00 2001 From: Gregor MacLennan Date: Sun, 29 Mar 2015 19:41:40 -0700 Subject: [PATCH 067/956] Add cargo animation to README For some reason the README only included one of @rhyzx 's brilliant animations, which really help explain `cargo` --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 6cfb922c5..88ecd7313 100644 --- a/README.md +++ b/README.md @@ -1154,7 +1154,7 @@ Creates a `cargo` object with the specified payload. Tasks added to the cargo will be processed altogether (up to the `payload` limit). If the `worker` is in progress, the task is queued until it becomes available. Once the `worker` has completed some tasks, each callback of those tasks is called. -Check out [this animation](https://camo.githubusercontent.com/6bbd36f4cf5b35a0f11a96dcd2e97711ffc2fb37/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f313637363837312f36383130382f62626330636662302d356632392d313165322d393734662d3333393763363464633835382e676966) for how `cargo` and `queue` work. +Check out [these](https://camo.githubusercontent.com/6bbd36f4cf5b35a0f11a96dcd2e97711ffc2fb37/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f313637363837312f36383130382f62626330636662302d356632392d313165322d393734662d3333393763363464633835382e676966) [animations](https://camo.githubusercontent.com/f4810e00e1c5f5f8addbe3e9f49064fd5d102699/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f313637363837312f36383130312f38346339323036362d356632392d313165322d383134662d3964336430323431336266642e676966) for how `cargo` and `queue` work. While [queue](#queue) passes only one task to one of a group of workers at a time, cargo passes an array of tasks to a single worker, repeating From 11bf48b4ff52823edb66e35d05571724accc02d3 Mon Sep 17 00:00:00 2001 From: Bao Date: Wed, 13 May 2015 00:02:30 -0700 Subject: [PATCH 068/956] Fixed issue with _eachLimit continuing to run after error --- lib/async.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/async.js b/lib/async.js index 394c41cad..b93e1781c 100644 --- a/lib/async.js +++ b/lib/async.js @@ -179,19 +179,21 @@ var completed = 0; var started = 0; var running = 0; + var errored = false; (function replenish () { if (completed >= arr.length) { return callback(); } - while (running < limit && started < arr.length) { + while (running < limit && started < arr.length && !errored) { started += 1; running += 1; iterator(arr[started - 1], function (err) { if (err) { callback(err); callback = function () {}; + errored = true; } else { completed += 1; From 88507339f6ac7094cb06653a015503221da510f9 Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Mon, 18 May 2015 19:26:55 -0700 Subject: [PATCH 069/956] Use regular license string --- package.json | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index f5debe2af..fb329d9fd 100644 --- a/package.json +++ b/package.json @@ -17,10 +17,7 @@ "bugs": { "url": "https://github.com/caolan/async/issues" }, - "license": { - "type": "MIT", - "url": "https://github.com/caolan/async/raw/master/LICENSE" - }, + "license": "MIT", "devDependencies": { "nodeunit": ">0.0.0", "uglify-js": "1.2.x", @@ -54,4 +51,4 @@ "tests" ] } -} \ No newline at end of file +} From a034c1af6667fc2987325cf5be8ba2577986d27a Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Mon, 18 May 2015 19:34:25 -0700 Subject: [PATCH 070/956] Update package manager sync script --- bower.json | 2 +- package.json | 2 +- support/sync-package-managers.js | 9 +++------ 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/bower.json b/bower.json index e6f1b42f5..18176881e 100644 --- a/bower.json +++ b/bower.json @@ -9,6 +9,7 @@ "utility", "module" ], + "license": "MIT", "repository": { "type": "git", "url": "https://github.com/caolan/async.git" @@ -24,7 +25,6 @@ "globals", "node" ], - "license": "MIT", "ignore": [ "**/.*", "node_modules", diff --git a/package.json b/package.json index fb329d9fd..1424c76cb 100644 --- a/package.json +++ b/package.json @@ -51,4 +51,4 @@ "tests" ] } -} +} \ No newline at end of file diff --git a/support/sync-package-managers.js b/support/sync-package-managers.js index 310bbe6e3..30cb7c2d0 100755 --- a/support/sync-package-managers.js +++ b/support/sync-package-managers.js @@ -13,8 +13,6 @@ var IGNORES = ['**/.*', 'node_modules', 'bower_components', 'test', 'tests']; var INCLUDES = ['lib/async.js', 'README.md', 'LICENSE']; var REPOSITORY_NAME = 'caolan/async'; -var LICENSE_NAME = packageJson.license.type; - packageJson.jam = { main: packageJson.main, include: INCLUDES, @@ -32,21 +30,20 @@ packageJson.volo = { var bowerSpecific = { moduleType: ['amd', 'globals', 'node'], - license: LICENSE_NAME, ignore: IGNORES, authors: [packageJson.author] }; var bowerInclude = ['name', 'description', 'version', 'main', 'keywords', - 'homepage', 'repository', 'devDependencies']; + 'license', 'homepage', 'repository', 'devDependencies']; var componentSpecific = { - license: LICENSE_NAME, repository: REPOSITORY_NAME, scripts: [packageJson.main] }; -var componentInclude = ['name', 'description', 'version', 'keywords']; +var componentInclude = ['name', 'description', 'version', 'keywords', + 'license']; var bowerJson = _.merge({}, _.pick(packageJson, bowerInclude), bowerSpecific); var componentJson = _.merge({}, _.pick(packageJson, componentInclude), componentSpecific); From de3a16091d5125384eff4a54deb3998b13c3814c Mon Sep 17 00:00:00 2001 From: Beau Gunderson Date: Mon, 18 May 2015 19:59:38 -0700 Subject: [PATCH 071/956] =?UTF-8?q?0.11=20=E2=86=92=200.12,=20add=20iojs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 05d299e67..6064ca092 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,5 @@ language: node_js node_js: - "0.10" - - "0.11" + - "0.12" + - "iojs" From 074e091ec0a02be00627437c7a467984dcf03e91 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Tue, 19 May 2015 17:37:48 -0700 Subject: [PATCH 072/956] fixed jshint issues, run linter on npm test --- .jshintrc | 13 ++++-- lib/async.js | 30 +++++++------- package.json | 12 +++--- test/test-async.js | 101 ++++++++++++++++++++++----------------------- 4 files changed, 79 insertions(+), 77 deletions(-) diff --git a/.jshintrc b/.jshintrc index 3a2825a2e..172f4917a 100644 --- a/.jshintrc +++ b/.jshintrc @@ -5,15 +5,20 @@ "indent": 4, "noarg": true, "undef": true, - "unused": true, "trailing": true, + "evil": true, + "laxcomma": true, // Relaxing options + "onevar": false, "asi": false, "eqnull": true, - "evil": true, "expr": false, - "laxcomma": true, "loopfunc": true, - "sub": true + "sub": true, + "browser": true, + "node": true, + "globals": { + "define": true + } } diff --git a/lib/async.js b/lib/async.js index 108bc9dc2..d6cd961dc 100644 --- a/lib/async.js +++ b/lib/async.js @@ -5,8 +5,6 @@ * Copyright 2010-2014 Caolan McMahon * Released under the MIT license */ -/*jshint onevar: false, indent:4 */ -/*global setImmediate: false, setTimeout: false, console: false */ (function () { var async = {}; @@ -40,7 +38,7 @@ if (called) throw new Error("Callback was already called."); called = true; fn.apply(root, arguments); - } + }; } //// cross-browser compatiblity functions //// @@ -79,7 +77,7 @@ }; var _forEachOf = function (object, iterator) { - for (key in object) { + for (var key in object) { if (object.hasOwnProperty(key)) { iterator(object[key], key); } @@ -233,7 +231,7 @@ async.forEachOf = async.eachOf = function (object, iterator, callback) { callback = callback || function () {}; var size = object.length || _keys(object).length; - var completed = 0 + var completed = 0; if (!size) { return callback(); } @@ -544,7 +542,7 @@ async.auto = function (tasks, callback) { callback = callback || noop; var keys = _keys(tasks); - var remainingTasks = keys.length + var remainingTasks = keys.length; if (!remainingTasks) { return callback(); } @@ -564,7 +562,7 @@ } }; var taskComplete = function () { - remainingTasks-- + remainingTasks--; _each(listeners.slice(0), function (fn) { fn(); }); @@ -660,9 +658,9 @@ data = data[data.length - 1]; (wrappedCallback || callback)(data.err, data.result); }); - } + }; // If a callback is passed, run this as a controll flow - return callback ? wrappedTask() : wrappedTask + return callback ? wrappedTask() : wrappedTask; }; async.waterfall = function (tasks, callback) { @@ -878,7 +876,7 @@ if (!_isArray(data)) { data = [data]; } - if(data.length == 0) { + if(data.length === 0) { // call drain immediately if there are no tasks return async.setImmediate(function() { if (q.drain) { @@ -975,7 +973,7 @@ function _compareTasks(a, b){ return a.priority - b.priority; - }; + } function _binarySearch(sequence, item, compare) { var beg = -1, @@ -998,7 +996,7 @@ if (!_isArray(data)) { data = [data]; } - if(data.length == 0) { + if(data.length === 0) { // call drain immediately if there are no tasks return async.setImmediate(function() { if (q.drain) { @@ -1071,9 +1069,9 @@ return; } - var ts = typeof payload === 'number' - ? tasks.splice(0, payload) - : tasks.splice(0, tasks.length); + var ts = typeof payload === 'number' ? + tasks.splice(0, payload) : + tasks.splice(0, tasks.length); var ds = _map(ts, function (task) { return task.data; @@ -1198,7 +1196,7 @@ var err = arguments[0]; var nextargs = Array.prototype.slice.call(arguments, 1); cb(err, nextargs); - }])) + }])); }, function (err, results) { callback.apply(that, [err].concat(results)); diff --git a/package.json b/package.json index 1424c76cb..ffb6491f9 100644 --- a/package.json +++ b/package.json @@ -19,10 +19,11 @@ }, "license": "MIT", "devDependencies": { - "nodeunit": ">0.0.0", - "uglify-js": "1.2.x", + "jshint": "~2.7.0", + "lodash": ">=2.4.1", "nodelint": ">0.0.0", - "lodash": ">=2.4.1" + "nodeunit": ">0.0.0", + "uglify-js": "1.2.x" }, "jam": { "main": "lib/async.js", @@ -36,7 +37,8 @@ ] }, "scripts": { - "test": "nodeunit test/test-async.js" + "test": "npm run-script lint && nodeunit test/test-async.js", + "lint": "jshint lib/async.js test/test-async.js" }, "spm": { "main": "lib/async.js" @@ -51,4 +53,4 @@ "tests" ] } -} \ No newline at end of file +} diff --git a/test/test-async.js b/test/test-async.js index e660c1ba7..6f6a74153 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -6,7 +6,7 @@ if (!Function.prototype.bind) { var self = this; return function () { self.apply(thisArg, args.concat(Array.prototype.slice.call(arguments))); - } + }; }; } @@ -526,7 +526,7 @@ exports['auto error should pass partial results'] = function(test) { // Issue 76 on github: https://github.com/caolan/async/issues#issue/76 exports['auto removeListener has side effect on loop iterator'] = function(test) { async.auto({ - task1: ['task3', function(callback) { test.done() }], + task1: ['task3', function(callback) { test.done(); }], task2: ['task3', function(callback) { /* by design: DON'T call callback */ }], task3: function(callback) { callback(); } }); @@ -573,7 +573,7 @@ exports['auto calls callback multiple times'] = function(test) { exports['auto modifying results causes final callback to run early'] = function(test) { async.auto({ task1: function(callback, results){ - results.inserted = true + results.inserted = true; callback(null, 'task1'); }, task2: function(callback){ @@ -588,8 +588,8 @@ exports['auto modifying results causes final callback to run early'] = function( } }, function(err, results){ - test.equal(results.inserted, true) - test.ok(results.task3, 'task3') + test.equal(results.inserted, true); + test.ok(results.task3, 'task3'); test.done(); }); }; @@ -623,18 +623,18 @@ exports['auto prevent dead-locks due to cyclic dependencies'] = function(test) { // Issue 306 on github: https://github.com/caolan/async/issues/306 exports['retry when attempt succeeds'] = function(test) { - var failed = 3 - var callCount = 0 - var expectedResult = 'success' + var failed = 3; + var callCount = 0; + var expectedResult = 'success'; function fn(callback, results) { - callCount++ - failed-- - if (!failed) callback(null, expectedResult) - else callback(true) // respond with error + callCount++; + failed--; + if (!failed) callback(null, expectedResult); + else callback(true); // respond with error } async.retry(fn, function(err, result){ - test.equal(callCount, 3, 'did not retry the correct number of times') - test.equal(result, expectedResult, 'did not return the expected result') + test.equal(callCount, 3, 'did not retry the correct number of times'); + test.equal(result, expectedResult, 'did not return the expected result'); test.done(); }); }; @@ -647,7 +647,7 @@ exports['retry when all attempts succeeds'] = function(test) { function fn(callback, results) { callCount++; callback(error + callCount, erroredResult + callCount); // respond with indexed values - }; + } async.retry(times, fn, function(err, result){ test.equal(callCount, 3, "did not retry the correct number of times"); test.equal(err, error + times, "Incorrect error was returned"); @@ -1511,7 +1511,7 @@ exports['forEachOfLimit synchronous'] = function(test){ exports['forEachOfLimit with array'] = function(test){ var args = []; - var arr = [ "a", "b" ] + var arr = [ "a", "b" ]; async.forEachOfLimit(arr, 1, forEachOfIterator.bind(this, args), function (err) { test.same(args, [ 0, "a", 1, "b" ]); test.done(); @@ -1663,7 +1663,7 @@ exports['reduce'] = function(test){ exports['reduce async with non-reference memo'] = function(test){ async.reduce([1,3,2], 0, function(a, x, callback){ - setTimeout(function(){callback(null, a + x)}, Math.random()*100); + setTimeout(function(){callback(null, a + x);}, Math.random()*100); }, function(err, result){ test.equals(result, 6); test.done(); @@ -1916,7 +1916,7 @@ exports['sortBy inverted'] = function(test){ exports['apply'] = function(test){ test.expect(6); var fn = function(){ - test.same(Array.prototype.slice.call(arguments), [1,2,3,4]) + test.same(Array.prototype.slice.call(arguments), [1,2,3,4]); }; async.apply(fn, 1, 2, 3, 4)(); async.apply(fn, 1, 2, 3)(4); @@ -1924,7 +1924,7 @@ exports['apply'] = function(test){ async.apply(fn, 1)(2, 3, 4); async.apply(fn)(1, 2, 3, 4); test.equals( - async.apply(function(name){return 'hello ' + name}, 'world')(), + async.apply(function(name){return 'hello ' + name;}, 'world')(), 'hello world' ); test.done(); @@ -1994,14 +1994,14 @@ var console_fn_tests = function(name){ exports['times'] = function(test) { - var indices = [] + var indices = []; async.times(5, function(n, next) { - next(null, n) + next(null, n); }, function(err, results) { - test.same(results, [0,1,2,3,4]) - test.done() - }) -} + test.same(results, [0,1,2,3,4]); + test.done(); + }); +}; exports['times'] = function(test){ var args = []; @@ -2089,9 +2089,6 @@ exports['nextTick in the browser'] = function(test){ call_order.push('one'); setTimeout(function(){ - if (typeof process !== 'undefined') { - process.nextTick = _nextTick; - } test.same(call_order, ['one','two']); }, 50); setTimeout(test.done, 100); @@ -2643,12 +2640,12 @@ exports['queue bulk task'] = function (test) { exports['queue idle'] = function(test) { var q = async.queue(function (task, callback) { // Queue is busy when workers are running - test.equal(q.idle(), false) + test.equal(q.idle(), false); callback(); }, 1); // Queue is idle before anything added - test.equal(q.idle(), true) + test.equal(q.idle(), true); q.unshift(4); q.unshift(3); @@ -2656,14 +2653,14 @@ exports['queue idle'] = function(test) { q.unshift(1); // Queue is busy when tasks added - test.equal(q.idle(), false) + test.equal(q.idle(), false); q.drain = function() { // Queue is idle after drain test.equal(q.idle(), true); test.done(); - } -} + }; +}; exports['queue pause'] = function(test) { var call_order = [], @@ -2716,7 +2713,7 @@ exports['queue pause'] = function(test) { ]); test.done(); }, 800); -} +}; exports['queue pause with concurrency'] = function(test) { var call_order = [], @@ -2763,7 +2760,7 @@ exports['queue pause with concurrency'] = function(test) { ]); test.done(); }, 800); -} +}; exports['queue kill'] = function (test) { var q = async.queue(function (task, callback) { @@ -2774,7 +2771,7 @@ exports['queue kill'] = function (test) { }, 1); q.drain = function() { test.ok(false, "Function should never be called"); - } + }; q.push(0); @@ -2783,7 +2780,7 @@ exports['queue kill'] = function (test) { setTimeout(function() { test.equal(q.length(), 0); test.done(); - }, 600) + }, 600); }; exports['priorityQueue'] = function (test) { @@ -3034,7 +3031,7 @@ exports['cargo drain once'] = function (test) { var drainCounter = 0; c.drain = function () { drainCounter++; - } + }; for(var i = 0; i < 10; i++){ c.push(i); @@ -3061,7 +3058,7 @@ exports['cargo drain twice'] = function (test) { var drainCounter = 0; c.drain = function () { drainCounter++; - } + }; loadCargo(); setTimeout(loadCargo, 500); @@ -3160,7 +3157,7 @@ exports['unmemoize'] = function(test) { }); }); }); -} +}; exports['unmemoize a not memoized function'] = function(test) { test.expect(1); @@ -3175,7 +3172,7 @@ exports['unmemoize a not memoized function'] = function(test) { }); test.done(); -} +}; exports['memoize error'] = function (test) { test.expect(1); @@ -3244,22 +3241,22 @@ exports['falsy return values in series'] = function (test) { async.nextTick(function() { callback(null, false); }); - }; + } function taskUndefined(callback) { async.nextTick(function() { callback(null, undefined); }); - }; + } function taskEmpty(callback) { async.nextTick(function() { callback(null); }); - }; + } function taskNull(callback) { async.nextTick(function() { callback(null, null); }); - }; + } async.series( [taskFalse, taskUndefined, taskEmpty, taskNull], function(err, results) { @@ -3279,22 +3276,22 @@ exports['falsy return values in parallel'] = function (test) { async.nextTick(function() { callback(null, false); }); - }; + } function taskUndefined(callback) { async.nextTick(function() { callback(null, undefined); }); - }; + } function taskEmpty(callback) { async.nextTick(function() { callback(null); }); - }; + } function taskNull(callback) { async.nextTick(function() { callback(null, null); }); - }; + } async.parallel( [taskFalse, taskUndefined, taskEmpty, taskNull], function(err, results) { @@ -3322,12 +3319,12 @@ exports['queue events'] = function(test) { calls.push('saturated'); }; q.empty = function() { - test.ok(q.length() == 0, 'queue should be empty now'); + test.ok(q.length() === 0, 'queue should be empty now'); calls.push('empty'); }; q.drain = function() { test.ok( - q.length() == 0 && q.running() == 0, + q.length() === 0 && q.running() === 0, 'queue should be empty now and no more workers should be running' ); calls.push('drain'); @@ -3365,7 +3362,7 @@ exports['queue empty'] = function(test) { q.drain = function() { test.ok( - q.length() == 0 && q.running() == 0, + q.length() === 0 && q.running() === 0, 'queue should be empty now and no more workers should be running' ); calls.push('drain'); From f49a63ffe8e0cbc9ba8b3da5f78d626a2188e66a Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Tue, 19 May 2015 19:06:09 -0700 Subject: [PATCH 073/956] added changelog --- CHANGELOG.md | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 000000000..c712934c4 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,8 @@ +# v0.10 + +- Started using a changelog! +- Added `forEachOf` for iterating over Objects (or to iterate Arrays with indexes) (#168 #704 #321) +- Detect deadlocks in `auto` (#663) +- Better support for require.js (#527) +- Doc fixes (#542 #596 #615 #628 #631 #690) +- Use single noop function internally (#546) From 8837212d58c354a7477e05d9450fd826c0c11fe0 Mon Sep 17 00:00:00 2001 From: Suguru Motegi Date: Wed, 20 May 2015 11:28:13 +0900 Subject: [PATCH 074/956] perf(slice): fix not to use `Array#slice` --- lib/async.js | 59 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 21 deletions(-) diff --git a/lib/async.js b/lib/async.js index d6cd961dc..47ab5a723 100644 --- a/lib/async.js +++ b/lib/async.js @@ -97,6 +97,23 @@ return keys; }; + var _baseSlice = function (arr, start) { + start = start || 0; + var index = -1; + var length = arr.length; + + if (start) { + length -= start; + length = length < 0 ? 0 : length; + } + var result = Array(length); + + while (++index < length) { + result[index] = arr[index + start]; + } + return result; + }; + //// exported async module functions //// //// nextTick implementation with browser-compatible fallback //// @@ -338,19 +355,19 @@ var doParallel = function (fn) { return function () { - var args = Array.prototype.slice.call(arguments); + var args = _baseSlice(arguments); return fn.apply(null, [async.each].concat(args)); }; }; var doParallelLimit = function(limit, fn) { return function () { - var args = Array.prototype.slice.call(arguments); + var args = _baseSlice(arguments); return fn.apply(null, [_eachLimit(limit)].concat(args)); }; }; var doSeries = function (fn) { return function () { - var args = Array.prototype.slice.call(arguments); + var args = _baseSlice(arguments); return fn.apply(null, [async.eachSeries].concat(args)); }; }; @@ -581,7 +598,7 @@ _each(keys, function (k) { var task = _isArray(tasks[k]) ? tasks[k]: [tasks[k]]; var taskCallback = function (err) { - var args = Array.prototype.slice.call(arguments, 1); + var args = _baseSlice(arguments, 1); if (args.length <= 1) { args = args[0]; } @@ -679,7 +696,7 @@ callback = noop; } else { - var args = Array.prototype.slice.call(arguments, 1); + var args = _baseSlice(arguments, 1); var next = iterator.next(); if (next) { args.push(wrapIterator(next)); @@ -702,7 +719,7 @@ eachfn.map(tasks, function (fn, callback) { if (fn) { fn(function (err) { - var args = Array.prototype.slice.call(arguments, 1); + var args = _baseSlice(arguments, 1); if (args.length <= 1) { args = args[0]; } @@ -715,7 +732,7 @@ var results = {}; eachfn.each(_keys(tasks), function (k, callback) { tasks[k](function (err) { - var args = Array.prototype.slice.call(arguments, 1); + var args = _baseSlice(arguments, 1); if (args.length <= 1) { args = args[0]; } @@ -742,7 +759,7 @@ async.mapSeries(tasks, function (fn, callback) { if (fn) { fn(function (err) { - var args = Array.prototype.slice.call(arguments, 1); + var args = _baseSlice(arguments, 1); if (args.length <= 1) { args = args[0]; } @@ -755,7 +772,7 @@ var results = {}; async.eachSeries(_keys(tasks), function (k, callback) { tasks[k](function (err) { - var args = Array.prototype.slice.call(arguments, 1); + var args = _baseSlice(arguments, 1); if (args.length <= 1) { args = args[0]; } @@ -785,10 +802,10 @@ }; async.apply = function (fn) { - var args = Array.prototype.slice.call(arguments, 1); + var args = _baseSlice(arguments, 1); return function () { return fn.apply( - null, args.concat(Array.prototype.slice.call(arguments)) + null, args.concat(_baseSlice(arguments)) ); }; }; @@ -826,7 +843,7 @@ if (err) { return callback(err); } - var args = Array.prototype.slice.call(arguments, 1); + var args = _baseSlice(arguments, 1); if (test.apply(null, args)) { async.doWhilst(iterator, test, callback); } @@ -855,7 +872,7 @@ if (err) { return callback(err); } - var args = Array.prototype.slice.call(arguments, 1); + var args = _baseSlice(arguments, 1); if (!test.apply(null, args)) { async.doUntil(iterator, test, callback); } @@ -1104,9 +1121,9 @@ var _console_fn = function (name) { return function (fn) { - var args = Array.prototype.slice.call(arguments, 1); + var args = _baseSlice(arguments, 1); fn.apply(null, args.concat([function (err) { - var args = Array.prototype.slice.call(arguments, 1); + var args = _baseSlice(arguments, 1); if (typeof console !== 'undefined') { if (err) { if (console.error) { @@ -1135,7 +1152,7 @@ return x; }; var memoized = function () { - var args = Array.prototype.slice.call(arguments); + var args = _baseSlice(arguments); var callback = args.pop(); var key = hasher.apply(null, args); if (key in memo) { @@ -1149,7 +1166,7 @@ else { queues[key] = [callback]; fn.apply(null, args.concat([function () { - memo[key] = arguments; + memo[key] = _baseSlice(arguments); var q = queues[key]; delete queues[key]; for (var i = 0, l = q.length; i < l; i++) { @@ -1189,12 +1206,12 @@ var fns = arguments; return function () { var that = this; - var args = Array.prototype.slice.call(arguments); + var args = _baseSlice(arguments); var callback = args.pop(); async.reduce(fns, args, function (newargs, fn, cb) { fn.apply(that, newargs.concat([function () { var err = arguments[0]; - var nextargs = Array.prototype.slice.call(arguments, 1); + var nextargs = _baseSlice(arguments, 1); cb(err, nextargs); }])); }, @@ -1211,7 +1228,7 @@ var _applyEach = function (eachfn, fns /*args...*/) { var go = function () { var that = this; - var args = Array.prototype.slice.call(arguments); + var args = _baseSlice(arguments); var callback = args.pop(); return eachfn(fns, function (fn, cb) { fn.apply(that, args.concat([cb])); @@ -1219,7 +1236,7 @@ callback); }; if (arguments.length > 2) { - var args = Array.prototype.slice.call(arguments, 2); + var args = _baseSlice(arguments, 2); return go.apply(this, args); } else { From a73eef321256b36f529dce57a1e6ad7efd34851a Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Tue, 19 May 2015 19:45:39 -0700 Subject: [PATCH 075/956] cleaning up build files --- .gitignore | 4 ++-- Makefile | 13 ++++++------- package.json | 1 - 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 8225baa4a..f06235c46 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -/node_modules -/dist +node_modules +dist diff --git a/Makefile b/Makefile index 4b0f23cbc..ac16a072a 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,8 @@ PACKAGE = asyncjs -NODEJS = $(if $(shell test -f /usr/bin/nodejs && echo "true"),nodejs,node) -CWD := "$(shell pwd)" -NODEUNIT = $(CWD)/node_modules/nodeunit/bin/nodeunit -UGLIFY = $(CWD)/node_modules/uglify-js/bin/uglifyjs -NODELINT = $(CWD)/node_modules/nodelint/nodelint +CWD := $(shell pwd) +NODEUNIT = "$(CWD)/node_modules/.bin/nodeunit" +UGLIFY = "$(CWD)/node_modules/.bin/uglifyjs" +JSHINT = "$(CWD)/node_modules/.bin/jshint" BUILDDIR = dist @@ -20,6 +19,6 @@ clean: rm -rf $(BUILDDIR) lint: - $(NODELINT) --config nodelint.cfg lib/async.js + $(JSHINT) lib/*.js test/*.js -.PHONY: test build all +.PHONY: test lint build all clean diff --git a/package.json b/package.json index ffb6491f9..7385df821 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,6 @@ "devDependencies": { "jshint": "~2.7.0", "lodash": ">=2.4.1", - "nodelint": ">0.0.0", "nodeunit": ">0.0.0", "uglify-js": "1.2.x" }, From 1f98d6a54dc8f74c582fa28f23893d2292cd7f81 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Tue, 19 May 2015 22:13:00 -0700 Subject: [PATCH 076/956] adding benchmark script --- .gitignore | 1 + nodelint.cfg | 4 -- package.json | 2 + perf/benchmark.js | 108 ++++++++++++++++++++++++++++++++++++++++++++++ perf/suites.js | 40 +++++++++++++++++ 5 files changed, 151 insertions(+), 4 deletions(-) delete mode 100644 nodelint.cfg create mode 100755 perf/benchmark.js create mode 100644 perf/suites.js diff --git a/.gitignore b/.gitignore index f06235c46..291d97eb0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules dist +perf/versions diff --git a/nodelint.cfg b/nodelint.cfg deleted file mode 100644 index 457a967e0..000000000 --- a/nodelint.cfg +++ /dev/null @@ -1,4 +0,0 @@ -var options = { - indent: 4, - onevar: false -}; diff --git a/package.json b/package.json index 7385df821..b79725083 100644 --- a/package.json +++ b/package.json @@ -19,8 +19,10 @@ }, "license": "MIT", "devDependencies": { + "benchmark": "~1.0.0", "jshint": "~2.7.0", "lodash": ">=2.4.1", + "mkdirp": "~0.5.1", "nodeunit": ">0.0.0", "uglify-js": "1.2.x" }, diff --git a/perf/benchmark.js b/perf/benchmark.js new file mode 100755 index 000000000..44dbc6133 --- /dev/null +++ b/perf/benchmark.js @@ -0,0 +1,108 @@ +#!/usr/bin/env node + +var Benchmark = require("benchmark"); +var benchOptions = {defer: true, minSamples: 7}; +var exec = require("child_process").exec; +var fs = require("fs"); +var mkdirp = require("mkdirp"); +var async = require("../"); +var suiteConfigs = require("./suites"); + +var version1 = process.argv[2] || require("../package.json").version; +var version2 = process.argv[3] || "current"; +var versionNames = [version1, version2]; +var versions; +var wins = [0, 0]; + +console.log("Comparing " + version1 + " with " + version2); +console.log("--------------------------------------"); + + +async.eachSeries(versionNames, cloneVersion, function (err) { + versions = versionNames.map(requireVersion); + + var suites = suiteConfigs.map(createSuite); + + async.eachSeries(suites, runSuite, function () { + var wins0 = wins[0].length; + var wins1 = wins[1].length; + + if (wins0 > wins1) { + console.log(versionNames[0] + " faster overall " + + "(" + wins0 + " wins vs. " + wins1 +" wins)"); + } else if (wins1 > wins0) { + console.log(versionNames[1] + " faster overall " + + "(" + wins1 + " wins vs. " + wins0 +" wins)"); + } else { + console.log("Both versions are equal"); + } + }); +}); + +function runSuite(suite, callback) { + suite.on("complete", function () { + callback(); + }).run({async: true}); +} + +function createSuite(suiteConfig) { + var suite = new Benchmark.Suite(); + + function addBench(version, versionName) { + var title = suiteConfig.name + " " + versionName; + suite.add(title, function (deferred) { + suiteConfig.fn(versions[0], deferred); + }, benchOptions); + } + + addBench(versions[0], versionNames[0]); + addBench(versions[1], versionNames[1]); + + return suite.on('cycle', function(event) { + console.log(event.target + ""); + }) + .on('complete', function() { + var fastest = this.filter('fastest'); + if (fastest.length === 2) { + console.log("Tie"); + } else { + console.log(fastest[0].name + ' is faster'); + var index = this.indexOf("fastest"); + wins[index]++; + } + console.log("--------------------------------------"); + }); + +} + +function requireVersion(tag) { + if (tag === "current") { + return async; + } + + return require("./versions/" + tag + "/"); +} + +function cloneVersion(tag, callback) { + if (tag === "current") return callback(); + + var versionDir = __dirname + "/versions/" + tag; + mkdirp.sync(versionDir); + fs.open(versionDir + "/package.json", "r", function (err, handle) { + if (!err) { + // version has already been cloned + fs.close(handle); + return callback(); + } + + var cmd = "git clone --branch " + tag + " ../ " + versionDir; + + exec(cmd, function (err, stdout, stderr) { + if (err) { + throw err; + } + callback(); + }); + + }); +} diff --git a/perf/suites.js b/perf/suites.js new file mode 100644 index 000000000..d9a7b19de --- /dev/null +++ b/perf/suites.js @@ -0,0 +1,40 @@ +module.exports = [ + { + name: "each(10)", + fn: function (async, deferred) { + async.each(Array(10), function (num, cb) { + async.setImmediate(cb); + }, done(deferred)); + } + }, + { + name: "each(10000)", + fn: function (async, deferred) { + async.each(Array(10000), function (num, cb) { + async.setImmediate(cb); + }, done(deferred)); + } + }, + { + name: "eachSeries(10)", + fn: function (async, deferred) { + async.eachSeries(Array(10), function (num, cb) { + async.setImmediate(cb); + }, done(deferred)); + } + }, + { + name: "eachSeries(10000)", + fn: function (async, deferred) { + async.eachSeries(Array(10000), function (num, cb) { + async.setImmediate(cb); + }, done(deferred)); + } + } +]; + +function done(deferred) { + return function () { + deferred.resolve(); + }; +} From 1b9200b16db246f660ee287b619e8edba3a19c0a Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Tue, 19 May 2015 23:30:29 -0700 Subject: [PATCH 077/956] fixed path in benchmark script --- perf/benchmark.js | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/perf/benchmark.js b/perf/benchmark.js index 44dbc6133..f31037bf0 100755 --- a/perf/benchmark.js +++ b/perf/benchmark.js @@ -1,9 +1,29 @@ #!/usr/bin/env node +/** + * Compare the performance of any two tagged versions of async. Can also + * compare any tag with what is in the current working directory. + * + * Usage: + * + * perf/benchmark.js 0.9.2 0.9.0 + * + * Compare version 0.9.0 with version 0.9.2 + * + * perf/benchmark.js 0.6.0 + * + * Compare version 0.6.0 with the current working version + * + * perf/benchmark.js + * + * Compare the current working version with the latest tagged version. + */ + var Benchmark = require("benchmark"); var benchOptions = {defer: true, minSamples: 7}; var exec = require("child_process").exec; var fs = require("fs"); +var path = require("path"); var mkdirp = require("mkdirp"); var async = require("../"); var suiteConfigs = require("./suites"); @@ -95,7 +115,9 @@ function cloneVersion(tag, callback) { return callback(); } - var cmd = "git clone --branch " + tag + " ../ " + versionDir; + var repoPath = path.join(__dirname, ".."); + + var cmd = "git clone --branch " + tag + " " + repoPath + " " + versionDir; exec(cmd, function (err, stdout, stderr) { if (err) { From 62971059e005198635b30b2b01920f054b5d6eaf Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Wed, 20 May 2015 00:31:56 -0700 Subject: [PATCH 078/956] added more benchmarks, improved benchmark reporting --- perf/benchmark.js | 41 +++++++++++------- perf/suites.js | 105 ++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 117 insertions(+), 29 deletions(-) diff --git a/perf/benchmark.js b/perf/benchmark.js index f31037bf0..8900062ac 100755 --- a/perf/benchmark.js +++ b/perf/benchmark.js @@ -19,8 +19,9 @@ * Compare the current working version with the latest tagged version. */ +var _ = require("lodash"); var Benchmark = require("benchmark"); -var benchOptions = {defer: true, minSamples: 7}; +var benchOptions = {defer: true, minSamples: 1, maxTime: 1}; var exec = require("child_process").exec; var fs = require("fs"); var path = require("path"); @@ -28,13 +29,15 @@ var mkdirp = require("mkdirp"); var async = require("../"); var suiteConfigs = require("./suites"); -var version1 = process.argv[2] || require("../package.json").version; -var version2 = process.argv[3] || "current"; -var versionNames = [version1, version2]; +var version0 = process.argv[2] || require("../package.json").version; +var version1 = process.argv[3] || "current"; +var versionNames = [version0, version1]; var versions; -var wins = [0, 0]; +var wins = {}; +wins[version0] = 0; +wins[version1] = 0; -console.log("Comparing " + version1 + " with " + version2); +console.log("Comparing " + version0 + " with " + version1); console.log("--------------------------------------"); @@ -44,14 +47,14 @@ async.eachSeries(versionNames, cloneVersion, function (err) { var suites = suiteConfigs.map(createSuite); async.eachSeries(suites, runSuite, function () { - var wins0 = wins[0].length; - var wins1 = wins[1].length; + var wins0 = wins[version0]; + var wins1 = wins[version1]; if (wins0 > wins1) { - console.log(versionNames[0] + " faster overall " + + console.log(version0 + " faster overall " + "(" + wins0 + " wins vs. " + wins1 +" wins)"); } else if (wins1 > wins0) { - console.log(versionNames[1] + " faster overall " + + console.log(version1 + " faster overall " + "(" + wins1 + " wins vs. " + wins0 +" wins)"); } else { console.log("Both versions are equal"); @@ -71,24 +74,30 @@ function createSuite(suiteConfig) { function addBench(version, versionName) { var title = suiteConfig.name + " " + versionName; suite.add(title, function (deferred) { - suiteConfig.fn(versions[0], deferred); - }, benchOptions); + suiteConfig.fn(versions[0], function () { + deferred.resolve(); + }); + }, _.extend({ + versionName: versionName, + setup: suiteConfig.setup + }, benchOptions)); } addBench(versions[0], versionNames[0]); addBench(versions[1], versionNames[1]); return suite.on('cycle', function(event) { - console.log(event.target + ""); + var mean = event.target.stats.mean * 1000; + console.log(event.target + ", " + mean.toFixed(1) + "ms per sample"); }) .on('complete', function() { var fastest = this.filter('fastest'); if (fastest.length === 2) { console.log("Tie"); } else { - console.log(fastest[0].name + ' is faster'); - var index = this.indexOf("fastest"); - wins[index]++; + var winner = fastest[0].options.versionName; + console.log(winner + ' is faster'); + wins[winner]++; } console.log("--------------------------------------"); }); diff --git a/perf/suites.js b/perf/suites.js index d9a7b19de..884b65ee3 100644 --- a/perf/suites.js +++ b/perf/suites.js @@ -1,40 +1,119 @@ +var _ = require("lodash"); +var parallel1000Funcs = _.range(1000).map(function () { + return function (cb) { cb(); }; +}); +var parallel10Funcs = _.range(10).map(function () { + return function (cb) { cb(); }; +}); + module.exports = [ { name: "each(10)", - fn: function (async, deferred) { + fn: function (async, done) { async.each(Array(10), function (num, cb) { async.setImmediate(cb); - }, done(deferred)); + }, done); } }, { name: "each(10000)", - fn: function (async, deferred) { + fn: function (async, done) { async.each(Array(10000), function (num, cb) { async.setImmediate(cb); - }, done(deferred)); + }, done); } }, { name: "eachSeries(10)", - fn: function (async, deferred) { + fn: function (async, done) { async.eachSeries(Array(10), function (num, cb) { async.setImmediate(cb); - }, done(deferred)); + }, done); } }, { name: "eachSeries(10000)", - fn: function (async, deferred) { + fn: function (async, done) { async.eachSeries(Array(10000), function (num, cb) { async.setImmediate(cb); - }, done(deferred)); + }, done); + } + }, + { + name: "parallel(10)", + fn: function (async, done) { + async.parallel(parallel10Funcs, done); + } + }, + { + name: "parallel(1000)", + fn: function (async, done) { + async.parallel(parallel1000Funcs, done); + } + }, + { + name: "queue(1000)", + fn: function (async, done) { + var numEntries = 1000; + var q = async.queue(worker, 1); + for (var i = 1; i <= numEntries; i++) { + q.push({num: i}); + } + function worker(task, callback) { + if (task.num === numEntries) { + return done(); + } + setImmediate(callback); + } + } + }, + { + name: "queue(30000)", + fn: function (async, done) { + var numEntries = 30000; + var q = async.queue(worker, 1); + for (var i = 1; i <= numEntries; i++) { + q.push({num: i}); + } + function worker(task, callback) { + if (task.num === numEntries) { + return done(); + } + setImmediate(callback); + } + } + }, + { + name: "queue(100000)", + fn: function (async, done) { + var numEntries = 100000; + var q = async.queue(worker, 1); + for (var i = 1; i <= numEntries; i++) { + q.push({num: i}); + } + function worker(task, callback) { + if (task.num === numEntries) { + return done(); + } + setImmediate(callback); + } + } + }, + { + name: "queue(200000)", + fn: function (async, done) { + var numEntries = 200000; + var q = async.queue(worker, 1); + for (var i = 1; i <= numEntries; i++) { + q.push({num: i}); + } + function worker(task, callback) { + if (task.num === numEntries) { + return done(); + } + setImmediate(callback); + } } } ]; -function done(deferred) { - return function () { - deferred.resolve(); - }; -} From 562f879df5f1fd2aa6d2b411e67357b78a9bc249 Mon Sep 17 00:00:00 2001 From: Vito Alexander Nordloh Date: Wed, 20 May 2015 23:22:52 +0200 Subject: [PATCH 079/956] intercept queue concurrency of 0 --- lib/async.js | 3 +++ test/test-async.js | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/lib/async.js b/lib/async.js index 47ab5a723..81916fd89 100644 --- a/lib/async.js +++ b/lib/async.js @@ -886,6 +886,9 @@ if (concurrency === undefined) { concurrency = 1; } + else if(concurrency === 0) { + throw new Error('Concurrency must not be zero'); + } function _insert(q, data, pos, callback) { if (!q.started){ q.started = true; diff --git a/test/test-async.js b/test/test-async.js index 6f6a74153..64c33f986 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -2453,6 +2453,13 @@ exports['queue default concurrency'] = function (test) { }; }; +exports['queue zero concurrency'] = function(test){ + test.throws(function () { + async.queue(function (task, callback) {}, 0); + }); + test.done(); +}; + exports['queue error propagation'] = function(test){ var results = []; From f5c6331d3e0c8af238a143b16cf7271902acf797 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Wed, 20 May 2015 15:31:25 -0700 Subject: [PATCH 080/956] improved benchmark code, added more benchmarks --- perf/benchmark.js | 60 +++++++++++++++++------ perf/suites.js | 122 ++++++++++++++++++++-------------------------- 2 files changed, 97 insertions(+), 85 deletions(-) diff --git a/perf/benchmark.js b/perf/benchmark.js index 8900062ac..903d04acc 100755 --- a/perf/benchmark.js +++ b/perf/benchmark.js @@ -21,7 +21,7 @@ var _ = require("lodash"); var Benchmark = require("benchmark"); -var benchOptions = {defer: true, minSamples: 1, maxTime: 1}; +var benchOptions = {defer: true, minSamples: 1, maxTime: 2}; var exec = require("child_process").exec; var fs = require("fs"); var path = require("path"); @@ -34,8 +34,9 @@ var version1 = process.argv[3] || "current"; var versionNames = [version0, version1]; var versions; var wins = {}; -wins[version0] = 0; -wins[version1] = 0; +var totalTime = {}; +totalTime[version0] = wins[version0] = 0; +totalTime[version1] = wins[version1] = 0; console.log("Comparing " + version0 + " with " + version1); console.log("--------------------------------------"); @@ -44,20 +45,26 @@ console.log("--------------------------------------"); async.eachSeries(versionNames, cloneVersion, function (err) { versions = versionNames.map(requireVersion); - var suites = suiteConfigs.map(createSuite); + var suites = suiteConfigs + .map(setDefaultOptions) + .reduce(handleMultipleArgs, []) + .map(setName) + .map(createSuite); async.eachSeries(suites, runSuite, function () { - var wins0 = wins[version0]; - var wins1 = wins[version1]; - - if (wins0 > wins1) { + var totalTime0 = Math.round(totalTime[version0]); + var totalTime1 = Math.round(totalTime[version1]); + + if ( Math.abs((totalTime0 / totalTime1) - 1) < 0.01) { + // if < 1% difference, we're likely within the margins of error + console.log("Both versions are about equal " + + "(" + totalTime0 + "ms total vs. " + totalTime1 + "ms total)"); + } else if (totalTime0 < totalTime1) { console.log(version0 + " faster overall " + - "(" + wins0 + " wins vs. " + wins1 +" wins)"); - } else if (wins1 > wins0) { + "(" + totalTime0 + "ms total vs. " + totalTime1 + "ms total)"); + } else if (totalTime1 < totalTime0) { console.log(version1 + " faster overall " + - "(" + wins1 + " wins vs. " + wins0 +" wins)"); - } else { - console.log("Both versions are equal"); + "(" + totalTime1 + "ms total vs. " + totalTime0 + "ms total)"); } }); }); @@ -68,27 +75,48 @@ function runSuite(suite, callback) { }).run({async: true}); } +function setDefaultOptions(suiteConfig) { + suiteConfig.args = suiteConfig.args || [[]]; + suiteConfig.setup = suiteConfig.setup || function () {}; + return suiteConfig; +} + +function handleMultipleArgs(list, suiteConfig) { + return list.concat(suiteConfig.args.map(function (args) { + return _.defaults({args: args}, suiteConfig); + })); +} + +function setName(suiteConfig) { + suiteConfig.name = suiteConfig.name + "(" + suiteConfig.args.join(",") + ")"; + return suiteConfig; +} + function createSuite(suiteConfig) { var suite = new Benchmark.Suite(); + var args = suiteConfig.args; function addBench(version, versionName) { - var title = suiteConfig.name + " " + versionName; - suite.add(title, function (deferred) { + var name = suiteConfig.name + " " + versionName; + suite.add(name, function (deferred) { suiteConfig.fn(versions[0], function () { deferred.resolve(); }); }, _.extend({ versionName: versionName, - setup: suiteConfig.setup + setup: _.partial.apply(null, [suiteConfig.setup].concat(args)) }, benchOptions)); } addBench(versions[0], versionNames[0]); addBench(versions[1], versionNames[1]); + return suite.on('cycle', function(event) { var mean = event.target.stats.mean * 1000; console.log(event.target + ", " + mean.toFixed(1) + "ms per sample"); + var version = event.target.options.versionName; + totalTime[version] += mean; }) .on('complete', function() { var fastest = this.filter('fastest'); diff --git a/perf/suites.js b/perf/suites.js index 884b65ee3..2e1beaa22 100644 --- a/perf/suites.js +++ b/perf/suites.js @@ -1,14 +1,14 @@ var _ = require("lodash"); -var parallel1000Funcs = _.range(1000).map(function () { - return function (cb) { cb(); }; -}); -var parallel10Funcs = _.range(10).map(function () { - return function (cb) { cb(); }; -}); +var tasks; module.exports = [ { - name: "each(10)", + name: "each", + // args lists are passed to the setup function + args: [[10], [300], [10000]], + setup: function(count) { + tasks = Array(count); + }, fn: function (async, done) { async.each(Array(10), function (num, cb) { async.setImmediate(cb); @@ -16,93 +16,77 @@ module.exports = [ } }, { - name: "each(10000)", + name: "eachSeries", + args: [[10], [300], [10000]], + setup: function(count) { + tasks = Array(count); + }, fn: function (async, done) { - async.each(Array(10000), function (num, cb) { + async.eachSeries(tasks, function (num, cb) { async.setImmediate(cb); }, done); } }, { - name: "eachSeries(10)", + name: "eachLimit", + args: [[10], [300], [10000]], + setup: function(count) { + tasks = Array(count); + }, fn: function (async, done) { - async.eachSeries(Array(10), function (num, cb) { + async.eachLimit(tasks, 4, function (num, cb) { async.setImmediate(cb); }, done); } }, { - name: "eachSeries(10000)", + name: "parallel", + args: [[10], [100], [1000]], + setup: function (count) { + tasks = _.range(count).map(function () { + return function (cb) { cb(); }; + }); + }, fn: function (async, done) { - async.eachSeries(Array(10000), function (num, cb) { - async.setImmediate(cb); - }, done); - } - }, - { - name: "parallel(10)", - fn: function (async, done) { - async.parallel(parallel10Funcs, done); - } - }, - { - name: "parallel(1000)", - fn: function (async, done) { - async.parallel(parallel1000Funcs, done); - } - }, - { - name: "queue(1000)", - fn: function (async, done) { - var numEntries = 1000; - var q = async.queue(worker, 1); - for (var i = 1; i <= numEntries; i++) { - q.push({num: i}); - } - function worker(task, callback) { - if (task.num === numEntries) { - return done(); - } - setImmediate(callback); - } + async.parallel(tasks, done); } }, { - name: "queue(30000)", + name: "series", + args: [[10], [100], [1000]], + setup: function (count) { + tasks = _.range(count).map(function () { + return function (cb) { cb(); }; + }); + }, fn: function (async, done) { - var numEntries = 30000; - var q = async.queue(worker, 1); - for (var i = 1; i <= numEntries; i++) { - q.push({num: i}); - } - function worker(task, callback) { - if (task.num === numEntries) { - return done(); - } - setImmediate(callback); - } + async.series(tasks, done); } }, { - name: "queue(100000)", + name: "waterfall", + args: [[10], [100], [1000]], + setup: function (count) { + tasks = [ + function (cb) { + return cb(null, 1); + } + ].concat(_.range(count).map(function (i) { + return function (arg, cb) { cb(null, i); }; + })); + }, fn: function (async, done) { - var numEntries = 100000; - var q = async.queue(worker, 1); - for (var i = 1; i <= numEntries; i++) { - q.push({num: i}); - } - function worker(task, callback) { - if (task.num === numEntries) { - return done(); - } - setImmediate(callback); - } + async.waterfall(tasks, done); } }, { - name: "queue(200000)", + name: "queue", + args: [[1000], [30000], [100000], [200000]], + setup: function (count) { + tasks = count; + }, fn: function (async, done) { - var numEntries = 200000; + var numEntries = tasks; var q = async.queue(worker, 1); for (var i = 1; i <= numEntries; i++) { q.push({num: i}); From 3ffbf2b2fddf68d824c98903c60a9d35b34570e6 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Wed, 20 May 2015 15:42:15 -0700 Subject: [PATCH 081/956] optmize internal _each, _map, _keys, _forEachOf functions --- lib/async.js | 41 ++++++++++++++++++----------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/lib/async.js b/lib/async.js index 81916fd89..a1650a7e1 100644 --- a/lib/async.js +++ b/lib/async.js @@ -50,26 +50,26 @@ }; var _each = function (arr, iterator) { - for (var i = 0; i < arr.length; i += 1) { - iterator(arr[i], i, arr); - } + var index = -1, + length = arr.length; + + while (++index < length) { + iterator(arr[index], index, arr); + } }; var _map = function (arr, iterator) { - if (arr.map) { - return arr.map(iterator); - } - var results = []; - _each(arr, function (x, i, a) { - results.push(iterator(x, i, a)); - }); - return results; + var index = -1, + length = arr.length, + result = Array(length); + + while (++index < length) { + result[index] = iterator(arr[index], index, arr); + } + return result; }; var _reduce = function (arr, iterator, memo) { - if (arr.reduce) { - return arr.reduce(iterator, memo); - } _each(arr, function (x, i, a) { memo = iterator(memo, x, i, a); }); @@ -77,17 +77,12 @@ }; var _forEachOf = function (object, iterator) { - for (var key in object) { - if (object.hasOwnProperty(key)) { - iterator(object[key], key); - } - } + _each(_keys(object), function (key) { + iterator(object[key], key); + }); }; - var _keys = function (obj) { - if (Object.keys) { - return Object.keys(obj); - } + var _keys = Object.keys || function (obj) { var keys = []; for (var k in obj) { if (obj.hasOwnProperty(k)) { From 39715f48f23dc36cb0e8ebe44dde16ca07a04b06 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Wed, 20 May 2015 15:46:46 -0700 Subject: [PATCH 082/956] update ignore files, update changelog --- .npmignore | 2 +- CHANGELOG.md | 12 +++++++++--- Makefile | 2 +- package.json | 5 +++-- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/.npmignore b/.npmignore index 392d66e4d..5fc10cfbf 100644 --- a/.npmignore +++ b/.npmignore @@ -1,7 +1,7 @@ deps dist test -nodelint.cfg +perf .npmignore .gitmodules Makefile diff --git a/CHANGELOG.md b/CHANGELOG.md index c712934c4..d3ce9d0e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,14 @@ -# v0.10 +# v1.0.0 -- Started using a changelog! -- Added `forEachOf` for iterating over Objects (or to iterate Arrays with indexes) (#168 #704 #321) +No known breaking changes, we are simply complying with semver from here on out. + +Changes: + +- Start using a changelog! +- Add `forEachOf` for iterating over Objects (or to iterate Arrays with indexes available) (#168 #704 #321) - Detect deadlocks in `auto` (#663) - Better support for require.js (#527) +- Throw if queue created with concurrency `0` (#714) - Doc fixes (#542 #596 #615 #628 #631 #690) - Use single noop function internally (#546) +- Optimize internal `_each`, `_map` and `_keys` functions. diff --git a/Makefile b/Makefile index ac16a072a..435623903 100644 --- a/Makefile +++ b/Makefile @@ -19,6 +19,6 @@ clean: rm -rf $(BUILDDIR) lint: - $(JSHINT) lib/*.js test/*.js + $(JSHINT) lib/*.js test/*.js perf/*.js .PHONY: test lint build all clean diff --git a/package.json b/package.json index b79725083..ec89faf3f 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ }, "scripts": { "test": "npm run-script lint && nodeunit test/test-async.js", - "lint": "jshint lib/async.js test/test-async.js" + "lint": "jshint lib/*.js test/*.js perf/*.js" }, "spm": { "main": "lib/async.js" @@ -51,7 +51,8 @@ "node_modules", "bower_components", "test", - "tests" + "tests", + "perf" ] } } From 91f6fb305f860086fbb388c7ae7665eea39fde74 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Wed, 20 May 2015 16:20:44 -0700 Subject: [PATCH 083/956] fix unneeded iteration in queue.resume. Fixes #758 --- lib/async.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/async.js b/lib/async.js index a1650a7e1..bbb349572 100644 --- a/lib/async.js +++ b/lib/async.js @@ -974,9 +974,10 @@ resume: function () { if (q.paused === false) { return; } q.paused = false; + var resumeCount = Math.min(q.concurrency, q.tasks.length); // Need to call q.process once per concurrent // worker to preserve full concurrency after pause - for (var w = 1; w <= q.concurrency; w++) { + for (var w = 1; w <= resumeCount; w++) { async.setImmediate(q.process); } } From 29e7141e85c3d4029a5ce09402ff369d4419bbbf Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Wed, 20 May 2015 16:22:35 -0700 Subject: [PATCH 084/956] change pronoun. Fixes #729 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cc55da212..7e5e15fc6 100644 --- a/README.md +++ b/README.md @@ -358,7 +358,7 @@ Like [`forEachOf`](#forEachOf), except the number of `iterator`s running at a gi Produces a new array of values by mapping each value in `arr` through the `iterator` function. The `iterator` is called with an item from `arr` and a callback for when it has finished processing. Each of these callback takes 2 arguments: -an `error`, and the transformed item from `arr`. If `iterator` passes an error to his +an `error`, and the transformed item from `arr`. If `iterator` passes an error to its callback, the main `callback` (for the `map` function) is immediately called with the error. Note, that since this function applies the `iterator` to each item in parallel, From e417af6c1e20374bb04468fe5a789fef701d0bec Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Wed, 20 May 2015 16:33:05 -0700 Subject: [PATCH 085/956] guard against setImmediate mocking. Fixes #609 #611 --- lib/async.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/async.js b/lib/async.js index bbb349572..1e76ee8bc 100644 --- a/lib/async.js +++ b/lib/async.js @@ -112,11 +112,18 @@ //// exported async module functions //// //// nextTick implementation with browser-compatible fallback //// + + // capture the global reference to guard against fakeTimer mocks + var _setImmediate; + if (typeof setImmediate === 'function') { + _setImmediate = setImmediate; + } + if (typeof process === 'undefined' || !(process.nextTick)) { - if (typeof setImmediate === 'function') { + if (_setImmediate) { async.nextTick = function (fn) { // not a direct alias for IE10 compatibility - setImmediate(fn); + _setImmediate(fn); }; async.setImmediate = async.nextTick; } @@ -129,10 +136,10 @@ } else { async.nextTick = process.nextTick; - if (typeof setImmediate !== 'undefined') { + if (_setImmediate) { async.setImmediate = function (fn) { // not a direct alias for IE10 compatibility - setImmediate(fn); + _setImmediate(fn); }; } else { From bb33062759891a2c68df44687eb0a7dc20a31810 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Wed, 20 May 2015 16:39:09 -0700 Subject: [PATCH 086/956] update changelog for 1.0.0 --- CHANGELOG.md | 4 +++- support/sync-package-managers.js | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3ce9d0e0..7d39c3788 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,8 @@ Changes: - Detect deadlocks in `auto` (#663) - Better support for require.js (#527) - Throw if queue created with concurrency `0` (#714) -- Doc fixes (#542 #596 #615 #628 #631 #690) +- Fix unneeded iteration in `queue.resume()` (#758) +- Guard against timer mocking overriding `setImmediate` (#609 #611) +- Miscellaneous doc fixes (#542 #596 #615 #628 #631 #690 #729) - Use single noop function internally (#546) - Optimize internal `_each`, `_map` and `_keys` functions. diff --git a/support/sync-package-managers.js b/support/sync-package-managers.js index 30cb7c2d0..5b261197a 100755 --- a/support/sync-package-managers.js +++ b/support/sync-package-managers.js @@ -43,7 +43,7 @@ var componentSpecific = { }; var componentInclude = ['name', 'description', 'version', 'keywords', - 'license']; + 'license', 'main']; var bowerJson = _.merge({}, _.pick(packageJson, bowerInclude), bowerSpecific); var componentJson = _.merge({}, _.pick(packageJson, componentInclude), componentSpecific); From cfa81645c9cb4011b23d1d1a445ad855762568e0 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Wed, 20 May 2015 16:40:25 -0700 Subject: [PATCH 087/956] v1.0.0 --- bower.json | 10 ++++++---- component.json | 3 ++- package.json | 7 +++---- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/bower.json b/bower.json index 18176881e..9e4156da1 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "async", "description": "Higher-order functions and common patterns for asynchronous code", - "version": "0.9.2", + "version": "1.0.0", "main": "lib/async.js", "keywords": [ "async", @@ -15,10 +15,12 @@ "url": "https://github.com/caolan/async.git" }, "devDependencies": { + "benchmark": "~1.0.0", + "jshint": "~2.7.0", + "lodash": ">=2.4.1", + "mkdirp": "~0.5.1", "nodeunit": ">0.0.0", - "uglify-js": "1.2.x", - "nodelint": ">0.0.0", - "lodash": ">=2.4.1" + "uglify-js": "1.2.x" }, "moduleType": [ "amd", diff --git a/component.json b/component.json index 5003a7c52..c876b0a6a 100644 --- a/component.json +++ b/component.json @@ -1,7 +1,7 @@ { "name": "async", "description": "Higher-order functions and common patterns for asynchronous code", - "version": "0.9.2", + "version": "1.0.0", "keywords": [ "async", "callback", @@ -9,6 +9,7 @@ "module" ], "license": "MIT", + "main": "lib/async.js", "repository": "caolan/async", "scripts": [ "lib/async.js" diff --git a/package.json b/package.json index ec89faf3f..14b05c6ce 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "description": "Higher-order functions and common patterns for asynchronous code", "main": "lib/async.js", "author": "Caolan McMahon", - "version": "0.9.2", + "version": "1.0.0", "keywords": [ "async", "callback", @@ -51,8 +51,7 @@ "node_modules", "bower_components", "test", - "tests", - "perf" + "tests" ] } -} +} \ No newline at end of file From a307b191ec50ecf22e3da157297b3628049c2068 Mon Sep 17 00:00:00 2001 From: Suguru Motegi Date: Thu, 21 May 2015 12:27:19 +0900 Subject: [PATCH 088/956] fix(benchmark): fix to enable to use second argument version --- perf/benchmark.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/perf/benchmark.js b/perf/benchmark.js index 903d04acc..f78ae046e 100755 --- a/perf/benchmark.js +++ b/perf/benchmark.js @@ -99,7 +99,7 @@ function createSuite(suiteConfig) { function addBench(version, versionName) { var name = suiteConfig.name + " " + versionName; suite.add(name, function (deferred) { - suiteConfig.fn(versions[0], function () { + suiteConfig.fn(version, function () { deferred.resolve(); }); }, _.extend({ From c8ed5ee14699f95c80a556b2fc667a96036e7bf1 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Wed, 20 May 2015 21:18:59 -0700 Subject: [PATCH 089/956] improved benchmark conclusions [ci skip] --- perf/benchmark.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/perf/benchmark.js b/perf/benchmark.js index f78ae046e..d69243a6e 100755 --- a/perf/benchmark.js +++ b/perf/benchmark.js @@ -55,6 +55,9 @@ async.eachSeries(versionNames, cloneVersion, function (err) { var totalTime0 = Math.round(totalTime[version0]); var totalTime1 = Math.round(totalTime[version1]); + var wins0 = Math.round(wins[version0]); + var wins1 = Math.round(wins[version1]); + if ( Math.abs((totalTime0 / totalTime1) - 1) < 0.01) { // if < 1% difference, we're likely within the margins of error console.log("Both versions are about equal " + @@ -66,6 +69,17 @@ async.eachSeries(versionNames, cloneVersion, function (err) { console.log(version1 + " faster overall " + "(" + totalTime1 + "ms total vs. " + totalTime0 + "ms total)"); } + + if (wins0 > wins1) { + console.log(version0 + " won more benchmarks " + + "(" + wins0 + " vs. " + wins1 + ")"); + } else if (wins1 > wins0) { + console.log(version1 + " won more benchmarks " + + "(" + wins1 + " vs. " + wins0 + ")"); + } else { + console.log("Both versions won the same number of benchmarks " + + "(" + wins0 + " vs. " + wins1 + ")"); + } }); }); From a2c8cc8648021c9bf7b43c13ed196788491eff87 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Wed, 20 May 2015 22:29:37 -0700 Subject: [PATCH 090/956] added additional detectSeries test, clarified docs. Closes #534 --- README.md | 11 +++++++---- test/test-async.js | 10 ++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 90514068e..01ab163bb 100644 --- a/README.md +++ b/README.md @@ -552,11 +552,11 @@ __Arguments__ * `arr` - An array to iterate over. * `iterator(item, callback)` - A truth test to apply to each item in `arr`. The iterator is passed a `callback(truthValue)` which must be called with a - boolean argument once it has completed. + boolean argument once it has completed. **Note: this callback does not take an error as its first argument.** * `callback(result)` - A callback which is called as soon as any iterator returns `true`, or after all the `iterator` functions have finished. Result will be the first item in the array that passes the truth test (iterator) or the - value `undefined` if none passed. + value `undefined` if none passed. **Note: this callback does not take an error as its first argument.** __Example__ @@ -644,12 +644,13 @@ __Arguments__ * `arr` - An array to iterate over. * `iterator(item, callback)` - A truth test to apply to each item in the array - in parallel. The iterator is passed a callback(truthValue) which must be + in parallel. The iterator is passed a `callback(truthValue)`` which must be called with a boolean argument once it has completed. * `callback(result)` - A callback which is called as soon as any iterator returns `true`, or after all the iterator functions have finished. Result will be either `true` or `false` depending on the values of the async tests. + **Note: the callbacks do not take an error as their first argument.** __Example__ ```js @@ -674,12 +675,14 @@ __Arguments__ * `arr` - An array to iterate over. * `iterator(item, callback)` - A truth test to apply to each item in the array - in parallel. The iterator is passed a callback(truthValue) which must be + in parallel. The iterator is passed a `callback(truthValue)` which must be called with a boolean argument once it has completed. * `callback(result)` - A callback which is called after all the `iterator` functions have finished. Result will be either `true` or `false` depending on the values of the async tests. + **Note: the callbacks do not take an error as their first argument.** + __Example__ ```js diff --git a/test/test-async.js b/test/test-async.js index 64c33f986..fe927ded1 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -1895,6 +1895,16 @@ exports['detectSeries - multiple matches'] = function(test){ }, 200); }; +exports['detectSeries - ensure stop'] = function (test) { + async.detectSeries([1, 2, 3, 4, 5], function (num, cb) { + if (num > 3) throw new Error("detectSeries did not stop iterating"); + cb(num === 3); + }, function (result) { + test.equals(result, 3); + test.done(); + }); +}; + exports['sortBy'] = function(test){ async.sortBy([{a:1},{a:15},{a:6}], function(x, callback){ setTimeout(function(){callback(null, x.a);}, 0); From 777439ff6a2a3b6a28d3cb0850246a87f3773445 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Wed, 20 May 2015 22:42:08 -0700 Subject: [PATCH 091/956] added test case for #489, #259 --- test/test-async.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/test-async.js b/test/test-async.js index fe927ded1..108d8b827 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -3391,6 +3391,25 @@ exports['queue empty'] = function(test) { q.push([]); }; +exports['queue saturated'] = function (test) { + var saturatedCalled = false; + var q = async.queue(function(task, cb) { + async.setImmediate(cb); + }, 2); + + q.saturated = function () { + saturatedCalled = true; + }; + q.drain = function () { + test.ok(saturatedCalled, "saturated not called"); + test.done(); + }; + + setTimeout(function () { + q.push(['foo', 'bar', 'baz', 'moo']); + }, 10); +}; + exports['queue started'] = function(test) { var calls = []; From e968c02b641f65b9ae458d2bc30a4668ef6fd82c Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Wed, 20 May 2015 23:19:04 -0700 Subject: [PATCH 092/956] pass err as null for consistency. Closes #491 --- lib/async.js | 32 ++++++++++++++--------------- test/test-async.js | 51 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 54 insertions(+), 29 deletions(-) diff --git a/lib/async.js b/lib/async.js index 1e76ee8bc..439ecc9eb 100644 --- a/lib/async.js +++ b/lib/async.js @@ -150,7 +150,7 @@ async.each = function (arr, iterator, callback) { callback = callback || noop; if (!arr.length) { - return callback(); + return callback(null); } var completed = 0; _each(arr, function (x) { @@ -164,7 +164,7 @@ else { completed += 1; if (completed >= arr.length) { - callback(); + callback(null); } } } @@ -174,7 +174,7 @@ async.eachSeries = function (arr, iterator, callback) { callback = callback || noop; if (!arr.length) { - return callback(); + return callback(null); } var completed = 0; var iterate = function () { @@ -186,7 +186,7 @@ else { completed += 1; if (completed >= arr.length) { - callback(); + callback(null); } else { iterate(); @@ -210,7 +210,7 @@ return function (arr, iterator, callback) { callback = callback || noop; if (!arr.length || limit <= 0) { - return callback(); + return callback(null); } var completed = 0; var started = 0; @@ -218,7 +218,7 @@ (function replenish () { if (completed >= arr.length) { - return callback(); + return callback(null); } while (running < limit && started < arr.length) { @@ -233,7 +233,7 @@ completed += 1; running -= 1; if (completed >= arr.length) { - callback(); + callback(null); } else { replenish(); @@ -252,7 +252,7 @@ var size = object.length || _keys(object).length; var completed = 0; if (!size) { - return callback(); + return callback(null); } _forEachOf(object, function (value, key) { iterator(object[key], key, function (err) { @@ -318,7 +318,7 @@ var keys = _keys(obj); var size = keys.length; if (!size || limit <= 0) { - return callback(); + return callback(null); } var completed = 0; var started = 0; @@ -342,7 +342,7 @@ completed += 1; running -= 1; if (completed >= size) { - callback(); + callback(null); } else { replenish(); @@ -416,7 +416,7 @@ callback(err); }); }, function (err) { - callback(err, memo); + callback(err || null, memo); }); }; // inject alias @@ -563,7 +563,7 @@ var keys = _keys(tasks); var remainingTasks = keys.length; if (!remainingTasks) { - return callback(); + return callback(null); } var results = {}; @@ -836,7 +836,7 @@ }); } else { - callback(); + callback(null); } }; @@ -850,7 +850,7 @@ async.doWhilst(iterator, test, callback); } else { - callback(); + callback(null); } }); }; @@ -865,7 +865,7 @@ }); } else { - callback(); + callback(null); } }; @@ -879,7 +879,7 @@ async.doUntil(iterator, test, callback); } else { - callback(); + callback(null); } }); }; diff --git a/test/test-async.js b/test/test-async.js index 108d8b827..13c3739f1 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -99,7 +99,7 @@ exports['forever'] = function (test) { }; exports['applyEach'] = function (test) { - test.expect(4); + test.expect(5); var call_order = []; var one = function (val, cb) { test.equal(val, 5); @@ -123,13 +123,14 @@ exports['applyEach'] = function (test) { }, 150); }; async.applyEach([one, two, three], 5, function (err) { + test.ok(err === null, err + " passed instead of 'null'"); test.same(call_order, ['two', 'one', 'three']); test.done(); }); }; exports['applyEachSeries'] = function (test) { - test.expect(4); + test.expect(5); var call_order = []; var one = function (val, cb) { test.equal(val, 5); @@ -153,6 +154,7 @@ exports['applyEachSeries'] = function (test) { }, 150); }; async.applyEachSeries([one, two, three], 5, function (err) { + test.ok(err === null, err + " passed instead of 'null'"); test.same(call_order, ['one', 'two', 'three']); test.done(); }); @@ -189,7 +191,7 @@ exports['applyEach partial application'] = function (test) { }; exports['compose'] = function (test) { - test.expect(4); + test.expect(5); var add2 = function (n, cb) { test.equal(n, 3); setTimeout(function () { @@ -213,6 +215,7 @@ exports['compose'] = function (test) { if (err) { return test.done(err); } + test.ok(err === null, err + " passed instead of 'null'"); test.equal(result, 16); test.done(); }); @@ -276,7 +279,7 @@ exports['compose binding'] = function (test) { }; exports['seq'] = function (test) { - test.expect(4); + test.expect(5); var add2 = function (n, cb) { test.equal(n, 3); setTimeout(function () { @@ -300,6 +303,7 @@ exports['seq'] = function (test) { if (err) { return test.done(err); } + test.ok(err === null, err + " passed instead of 'null'"); test.equal(result, 16); test.done(); }); @@ -398,6 +402,7 @@ exports['auto'] = function(test){ }] }, function(err){ + test.ok(err === null, err + " passed instead of 'null'"); test.same(callOrder, ['task2','task6','task3','task5','task1','task4']); test.done(); }); @@ -471,6 +476,7 @@ exports['auto results'] = function(test){ exports['auto empty object'] = function(test){ async.auto({}, function(err){ + test.ok(err === null, err + " passed instead of 'null'"); test.done(); }); }; @@ -633,6 +639,7 @@ exports['retry when attempt succeeds'] = function(test) { else callback(true); // respond with error } async.retry(fn, function(err, result){ + test.ok(err === null, err + " passed instead of 'null'"); test.equal(callCount, 3, 'did not retry the correct number of times'); test.equal(result, expectedResult, 'did not return the expected result'); test.done(); @@ -678,7 +685,7 @@ exports['retry as an embedded task'] = function(test) { }; exports['waterfall'] = function(test){ - test.expect(6); + test.expect(7); var call_order = []; async.waterfall([ function(callback){ @@ -704,6 +711,7 @@ exports['waterfall'] = function(test){ callback(null, 'test'); } ], function(err){ + test.ok(err === null, err + " passed instead of 'null'"); test.done(); }); }; @@ -843,7 +851,7 @@ exports['parallel'] = function(test){ } ], function(err, results){ - test.equals(err, null); + test.ok(err === null, err + " passed instead of 'null'"); test.same(call_order, [3,1,2]); test.same(results, [1,2,[3,3]]); test.done(); @@ -852,7 +860,7 @@ exports['parallel'] = function(test){ exports['parallel empty array'] = function(test){ async.parallel([], function(err, results){ - test.equals(err, null); + test.ok(err === null, err + " passed instead of 'null'"); test.same(results, []); test.done(); }); @@ -918,7 +926,7 @@ exports['parallel limit'] = function(test){ ], 2, function(err, results){ - test.equals(err, null); + test.ok(err === null, err + " passed instead of 'null'"); test.same(call_order, [1,3,2]); test.same(results, [1,2,[3,3]]); test.done(); @@ -927,7 +935,7 @@ exports['parallel limit'] = function(test){ exports['parallel limit empty array'] = function(test){ async.parallelLimit([], 2, function(err, results){ - test.equals(err, null); + test.ok(err === null, err + " passed instead of 'null'"); test.same(results, []); test.done(); }); @@ -1020,7 +1028,7 @@ exports['series'] = function(test){ } ], function(err, results){ - test.equals(err, null); + test.ok(err === null, err + " passed instead of 'null'"); test.same(results, [1,2,[3,3]]); test.same(call_order, [1,2,3]); test.done(); @@ -1159,6 +1167,7 @@ exports['iterator.next'] = function(test){ exports['each'] = function(test){ var args = []; async.each([1,3,2], eachIterator.bind(this, args), function(err){ + test.ok(err === null, err + " passed instead of 'null'"); test.same(args, [1,2,3]); test.done(); }); @@ -1209,6 +1218,7 @@ exports['forEach alias'] = function (test) { exports['forEachOf'] = function(test){ var args = []; async.forEachOf({ a: 1, b: 2 }, forEachOfIterator.bind(this, args), function(err){ + test.ok(err === null, err + " passed instead of 'null'"); test.same(args, ["a", 1, "b", 2]); test.done(); }); @@ -1250,6 +1260,7 @@ exports['forEachOf with array'] = function(test){ exports['eachSeries'] = function(test){ var args = []; async.eachSeries([1,3,2], eachIterator.bind(this, args), function(err){ + test.ok(err === null, err + " passed instead of 'null'"); test.same(args, [1,3,2]); test.done(); }); @@ -1293,6 +1304,7 @@ exports['eachLimit'] = function(test){ callback(); }, x*5); }, function(err){ + test.ok(err === null, err + " passed instead of 'null'"); test.same(args, arr); test.done(); }); @@ -1379,6 +1391,7 @@ exports['forEachSeries alias'] = function (test) { exports['forEachOfSeries'] = function(test){ var args = []; async.forEachOfSeries({ a: 1, b: 2 }, forEachOfIterator.bind(this, args), function(err){ + test.ok(err === null, err + " passed instead of 'null'"); test.same(args, [ "a", 1, "b", 2 ]); test.done(); }); @@ -1434,6 +1447,7 @@ exports['forEachOfLimit'] = function(test){ callback(); }, value * 5); }, function(err){ + test.ok(err === null, err + " passed instead of 'null'"); test.same(args, [ 1, "a", 2, "b", 3, "c", 4, "d" ]); test.done(); }); @@ -1521,6 +1535,7 @@ exports['forEachOfLimit with array'] = function(test){ exports['map'] = function(test){ var call_order = []; async.map([1,3,2], mapIterator.bind(this, call_order), function(err, results){ + test.ok(err === null, err + " passed instead of 'null'"); test.same(call_order, [1,2,3]); test.same(results, [2,6,4]); test.done(); @@ -1564,6 +1579,7 @@ exports['map error'] = function(test){ exports['mapSeries'] = function(test){ var call_order = []; async.mapSeries([1,3,2], mapIterator.bind(this, call_order), function(err, results){ + test.ok(err === null, err + " passed instead of 'null'"); test.same(call_order, [1,3,2]); test.same(results, [2,6,4]); test.done(); @@ -1584,6 +1600,7 @@ exports['mapSeries error'] = function(test){ exports['mapLimit'] = function(test){ var call_order = []; async.mapLimit([2,4,3], 2, mapIterator.bind(this, call_order), function(err, results){ + test.ok(err === null, err + " passed instead of 'null'"); test.same(call_order, [2,4,3]); test.same(results, [4,8,6]); test.done(); @@ -1655,6 +1672,7 @@ exports['reduce'] = function(test){ call_order.push(x); callback(null, a + x); }, function(err, result){ + test.ok(err === null, err + " passed instead of 'null'"); test.equals(result, 6); test.same(call_order, [1,2,3]); test.done(); @@ -1909,6 +1927,7 @@ exports['sortBy'] = function(test){ async.sortBy([{a:1},{a:15},{a:6}], function(x, callback){ setTimeout(function(){callback(null, x.a);}, 0); }, function(err, result){ + test.ok(err === null, err + " passed instead of 'null'"); test.same(result, [{a:1},{a:6},{a:15}]); test.done(); }); @@ -2008,6 +2027,7 @@ exports['times'] = function(test) { async.times(5, function(n, next) { next(null, n); }, function(err, results) { + test.ok(err === null, err + " passed instead of 'null'"); test.same(results, [0,1,2,3,4]); test.done(); }); @@ -2151,7 +2171,7 @@ exports['concat'] = function(test){ async.concat([1,3,2], iterator, function(err, results){ test.same(results, [1,2,1,3,2,1]); test.same(call_order, [1,2,3]); - test.ok(!err); + test.ok(err === null, err + " passed instead of 'null'"); test.done(); }); }; @@ -2182,7 +2202,7 @@ exports['concatSeries'] = function(test){ async.concatSeries([1,3,2], iterator, function(err, results){ test.same(results, [1,3,2,1,2,1]); test.same(call_order, [1,3,2]); - test.ok(!err); + test.ok(err === null, err + " passed instead of 'null'"); test.done(); }); }; @@ -2202,6 +2222,7 @@ exports['until'] = function (test) { cb(); }, function (err) { + test.ok(err === null, err + " passed instead of 'null'"); test.same(call_order, [ ['test', 0], ['iterator', 0], ['test', 1], @@ -2230,6 +2251,7 @@ exports['doUntil'] = function (test) { return (count == 5); }, function (err) { + test.ok(err === null, err + " passed instead of 'null'"); test.same(call_order, [ ['iterator', 0], ['test', 1], ['iterator', 1], ['test', 2], @@ -2285,6 +2307,7 @@ exports['whilst'] = function (test) { cb(); }, function (err) { + test.ok(err === null, err + " passed instead of 'null'"); test.same(call_order, [ ['test', 0], ['iterator', 0], ['test', 1], @@ -2314,6 +2337,7 @@ exports['doWhilst'] = function (test) { return (count < 5); }, function (err) { + test.ok(err === null, err + " passed instead of 'null'"); test.same(call_order, [ ['iterator', 0], ['test', 1], ['iterator', 1], ['test', 2], @@ -3087,7 +3111,7 @@ exports['cargo drain twice'] = function (test) { }; exports['memoize'] = function (test) { - test.expect(4); + test.expect(5); var call_order = []; var fn = function (arg1, arg2, callback) { @@ -3099,6 +3123,7 @@ exports['memoize'] = function (test) { var fn2 = async.memoize(fn); fn2(1, 2, function (err, result) { + test.ok(err === null, err + " passed instead of 'null'"); test.equal(result, 3); fn2(1, 2, function (err, result) { test.equal(result, 3); From 651232ed9f4779b6cecf58cbeb1c6b99e56f16b5 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Wed, 20 May 2015 23:33:57 -0700 Subject: [PATCH 093/956] added tests for #508 and #512 --- test/test-async.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/test/test-async.js b/test/test-async.js index 13c3739f1..7c325c89f 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -2790,6 +2790,10 @@ exports['queue pause with concurrency'] = function(test) { test.equal(q.paused, false); }, resume_timeout); + setTimeout(function () { + test.equal(q.running(), 2); + }, resume_timeout + 10); + setTimeout(function () { test.same(call_order, [ 'process 1', 'timeout 100', @@ -2803,6 +2807,30 @@ exports['queue pause with concurrency'] = function(test) { }, 800); }; +exports['queue start paused'] = function (test) { + var q = async.queue(function (task, callback) { + setTimeout(function () { + callback(); + }, 10); + }, 2); + q.pause(); + + q.push([1, 2, 3]); + + setTimeout(function () { + q.resume(); + }, 10); + + setTimeout(function () { + test.equal(q.running(), 2); + q.resume(); + }, 15); + + q.drain = function () { + test.done(); + }; +}; + exports['queue kill'] = function (test) { var q = async.queue(function (task, callback) { setTimeout(function () { From 78a77f4c8306c2783d33309220451a15fde1ea02 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Wed, 20 May 2015 23:43:03 -0700 Subject: [PATCH 094/956] clarify queue docs. closes #589 and #599 --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 01ab163bb..4e0b9fca1 100644 --- a/README.md +++ b/README.md @@ -1128,7 +1128,7 @@ The same as [`applyEach`](#applyEach) only the functions are applied in series. --------------------------------------- -### queue(worker, concurrency) +### queue(worker, [concurrency]) Creates a `queue` object with the specified `concurrency`. Tasks added to the `queue` are processed in parallel (up to the `concurrency` limit). If all @@ -1139,9 +1139,9 @@ __Arguments__ * `worker(task, callback)` - An asynchronous function for processing a queued task, which must call its `callback(err)` argument when finished, with an - optional `error` as an argument. + optional `error` as an argument. If you want to handle errors from an individual task, pass a callback to `q.push()`. * `concurrency` - An `integer` for determining how many `worker` functions should be - run in parallel. + run in parallel. If omitted, the concurrency defaults to `1`. If the concurrency is `0`, an error is thrown. __Queue objects__ From 0da3ebbaeb1ac79dac4c156a96770005313cd667 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Thu, 21 May 2015 21:53:07 -0700 Subject: [PATCH 095/956] added yargs to benchmark runner, and grep options --- package.json | 5 +++-- perf/benchmark.js | 53 ++++++++++++++++++++++++++++------------------- 2 files changed, 35 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 14b05c6ce..8e5d203ef 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "lodash": ">=2.4.1", "mkdirp": "~0.5.1", "nodeunit": ">0.0.0", - "uglify-js": "1.2.x" + "uglify-js": "1.2.x", + "yargs": "~3.9.1" }, "jam": { "main": "lib/async.js", @@ -54,4 +55,4 @@ "tests" ] } -} \ No newline at end of file +} diff --git a/perf/benchmark.js b/perf/benchmark.js index d69243a6e..6ae3d0436 100755 --- a/perf/benchmark.js +++ b/perf/benchmark.js @@ -1,24 +1,5 @@ #!/usr/bin/env node -/** - * Compare the performance of any two tagged versions of async. Can also - * compare any tag with what is in the current working directory. - * - * Usage: - * - * perf/benchmark.js 0.9.2 0.9.0 - * - * Compare version 0.9.0 with version 0.9.2 - * - * perf/benchmark.js 0.6.0 - * - * Compare version 0.6.0 with the current working version - * - * perf/benchmark.js - * - * Compare the current working version with the latest tagged version. - */ - var _ = require("lodash"); var Benchmark = require("benchmark"); var benchOptions = {defer: true, minSamples: 1, maxTime: 2}; @@ -29,8 +10,28 @@ var mkdirp = require("mkdirp"); var async = require("../"); var suiteConfigs = require("./suites"); -var version0 = process.argv[2] || require("../package.json").version; -var version1 = process.argv[3] || "current"; +var args = require("yargs") + .usage("Usage: $0 [options] [tag1] [tag2]") + .describe("g", "run only benchmarks whose names match this regex") + .alias("g", "grep") + .default("g", ".*") + .describe("i", "skip benchmarks whose names match this regex") + .alias("g", "reject") + .default("i", "^$") + .help('h') + .alias('h', 'help') + .example('$0 0.9.2 0.9.0', 'Compare v0.9.2 with v0.9.0') + .example('$0 0.9.2', 'Compare v0.9.2 with the current working version') + .example('$0', 'Compare the latest tag with the current working version') + .example('$0 -g each', 'only run the each(), eachLimit() and eachSeries() benchmarks') + .example('') + .argv; + +var grep = new RegExp(args.g, "i"); +var reject = new RegExp(args.i, "i"); + +var version0 = args._[0] || require("../package.json").version; +var version1 = args._[1] || "current"; var versionNames = [version0, version1]; var versions; var wins = {}; @@ -49,6 +50,8 @@ async.eachSeries(versionNames, cloneVersion, function (err) { .map(setDefaultOptions) .reduce(handleMultipleArgs, []) .map(setName) + .filter(matchesGrep) + .filter(doesNotMatch) .map(createSuite); async.eachSeries(suites, runSuite, function () { @@ -106,6 +109,14 @@ function setName(suiteConfig) { return suiteConfig; } +function matchesGrep(suiteConfig) { + return !!grep.exec(suiteConfig.name); +} + +function doesNotMatch(suiteConfig) { + return !reject.exec(suiteConfig.name); +} + function createSuite(suiteConfig) { var suite = new Benchmark.Suite(); var args = suiteConfig.args; From 5b6f080710fb4dbb6ef753d8680a2396ce73f8f8 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Thu, 21 May 2015 22:11:15 -0700 Subject: [PATCH 096/956] added benchmarks for map and eachOf --- perf/suites.js | 76 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/perf/suites.js b/perf/suites.js index 2e1beaa22..6504ebc3a 100644 --- a/perf/suites.js +++ b/perf/suites.js @@ -10,7 +10,7 @@ module.exports = [ tasks = Array(count); }, fn: function (async, done) { - async.each(Array(10), function (num, cb) { + async.each(tasks, function (num, cb) { async.setImmediate(cb); }, done); } @@ -39,6 +39,80 @@ module.exports = [ }, done); } }, + { + name: "map", + // args lists are passed to the setup function + args: [[10], [300], [10000]], + setup: function(count) { + tasks = Array(count); + }, + fn: function (async, done) { + async.map(Array(10), function (num, cb) { + async.setImmediate(cb); + }, done); + } + }, + { + name: "mapSeries", + args: [[10], [300], [10000]], + setup: function(count) { + tasks = Array(count); + }, + fn: function (async, done) { + async.mapSeries(tasks, function (num, cb) { + async.setImmediate(cb); + }, done); + } + }, + { + name: "mapLimit", + args: [[10], [300], [10000]], + setup: function(count) { + tasks = Array(count); + }, + fn: function (async, done) { + async.mapLimit(tasks, 4, function (num, cb) { + async.setImmediate(cb); + }, done); + } + }, + { + name: "eachOf", + // args lists are passed to the setup function + args: [[10], [300], [10000]], + setup: function(count) { + tasks = _.range(count); + }, + fn: function (async, done) { + async.eachOf(tasks, function (num, i, cb) { + async.setImmediate(cb); + }, done); + } + }, + { + name: "eachOfSeries", + args: [[10], [300], [10000]], + setup: function(count) { + tasks = _.range(count); + }, + fn: function (async, done) { + async.eachOfSeries(tasks, function (num, i, cb) { + async.setImmediate(cb); + }, done); + } + }, + { + name: "eachOfLimit", + args: [[10], [300], [10000]], + setup: function(count) { + tasks = _.range(count); + }, + fn: function (async, done) { + async.eachOfLimit(tasks, 4, function (num, i, cb) { + async.setImmediate(cb); + }, done); + } + }, { name: "parallel", args: [[10], [100], [1000]], From 3a1aff02096b12c0ed8669c3349f1659d96b4e5b Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Thu, 21 May 2015 23:15:47 -0700 Subject: [PATCH 097/956] split waterfall tests into a group --- test/test-async.js | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/test/test-async.js b/test/test-async.js index 7c325c89f..1cb860ebc 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -684,7 +684,9 @@ exports['retry as an embedded task'] = function(test) { }); }; -exports['waterfall'] = function(test){ +exports['waterfall'] = { + +'basic': function(test){ test.expect(7); var call_order = []; async.waterfall([ @@ -714,29 +716,29 @@ exports['waterfall'] = function(test){ test.ok(err === null, err + " passed instead of 'null'"); test.done(); }); -}; +}, -exports['waterfall empty array'] = function(test){ +'empty array': function(test){ async.waterfall([], function(err){ test.done(); }); -}; +}, -exports['waterfall non-array'] = function(test){ +'non-array': function(test){ async.waterfall({}, function(err){ test.equals(err.message, 'First argument to waterfall must be an array of functions'); test.done(); }); -}; +}, -exports['waterfall no callback'] = function(test){ +'no callback': function(test){ async.waterfall([ function(callback){callback();}, function(callback){callback(); test.done();} ]); -}; +}, -exports['waterfall async'] = function(test){ +'async': function(test){ var call_order = []; async.waterfall([ function(callback){ @@ -753,9 +755,9 @@ exports['waterfall async'] = function(test){ test.done(); } ]); -}; +}, -exports['waterfall error'] = function(test){ +'error': function(test){ test.expect(1); async.waterfall([ function(callback){ @@ -769,9 +771,9 @@ exports['waterfall error'] = function(test){ test.equals(err, 'error'); }); setTimeout(test.done, 50); -}; +}, -exports['waterfall multiple callback calls'] = function(test){ +'multiple callback calls': function(test){ var call_order = []; var arr = [ function(callback){ @@ -798,9 +800,9 @@ exports['waterfall multiple callback calls'] = function(test){ } ]; async.waterfall(arr); -}; +}, -exports['waterfall call in another context'] = function(test) { +'call in another context': function(test) { if (typeof process === 'undefined') { // node only test test.done(); @@ -825,8 +827,9 @@ exports['waterfall call in another context'] = function(test) { }).toString() + "())"; vm.runInNewContext(fn, sandbox); -}; +} +}; exports['parallel'] = function(test){ var call_order = []; From 7be7cc6159fa03f78bb07415f547f693cc61f3e8 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Thu, 21 May 2015 23:45:54 -0700 Subject: [PATCH 098/956] added deferral benchmarks --- perf/suites.js | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/perf/suites.js b/perf/suites.js index 6504ebc3a..b735a5f85 100644 --- a/perf/suites.js +++ b/perf/suites.js @@ -172,6 +172,42 @@ module.exports = [ setImmediate(callback); } } + }, + { + name: "defer none", + fn: function (async, done) { + done(); + } + }, + { + name: "defer nextTick", + fn: function (async, done) { + process.nextTick(done); + } + }, + { + name: "defer setImmediate", + fn: function (async, done) { + setImmediate(done); + } + }, + { + name: "defer async.nextTick", + fn: function (async, done) { + async.nextTick(done); + } + }, + { + name: "defer async.setImmediate", + fn: function (async, done) { + async.setImmediate(done); + } + }, + { + name: "defer setTimeout", + fn: function (async, done) { + setTimeout(done, 0); + } } ]; From eeb33e687ba6e87f89f0452005571499859c6056 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Thu, 21 May 2015 23:59:41 -0700 Subject: [PATCH 099/956] update benchmark to 2.0.0-pre for more accurate tests --- package.json | 2 +- perf/suites.js | 6 ------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/package.json b/package.json index 8e5d203ef..6e7282be7 100644 --- a/package.json +++ b/package.json @@ -19,7 +19,7 @@ }, "license": "MIT", "devDependencies": { - "benchmark": "~1.0.0", + "benchmark": "bestiejs/benchmark.js", "jshint": "~2.7.0", "lodash": ">=2.4.1", "mkdirp": "~0.5.1", diff --git a/perf/suites.js b/perf/suites.js index b735a5f85..6b00b2535 100644 --- a/perf/suites.js +++ b/perf/suites.js @@ -173,12 +173,6 @@ module.exports = [ } } }, - { - name: "defer none", - fn: function (async, done) { - done(); - } - }, { name: "defer nextTick", fn: function (async, done) { From a44d11ca95304dca673e6487050e630c2b2d87ee Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Fri, 22 May 2015 00:34:00 -0700 Subject: [PATCH 100/956] dezalgo'd benchmarks, use toPrecision in reporting --- perf/benchmark.js | 10 +++++----- perf/suites.js | 20 +++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/perf/benchmark.js b/perf/benchmark.js index 6ae3d0436..da5921647 100755 --- a/perf/benchmark.js +++ b/perf/benchmark.js @@ -55,11 +55,11 @@ async.eachSeries(versionNames, cloneVersion, function (err) { .map(createSuite); async.eachSeries(suites, runSuite, function () { - var totalTime0 = Math.round(totalTime[version0]); - var totalTime1 = Math.round(totalTime[version1]); + var totalTime0 = +totalTime[version0].toPrecision(3); + var totalTime1 = +totalTime[version1].toPrecision(3); - var wins0 = Math.round(wins[version0]); - var wins1 = Math.round(wins[version1]); + var wins0 = wins[version0]; + var wins1 = wins[version1]; if ( Math.abs((totalTime0 / totalTime1) - 1) < 0.01) { // if < 1% difference, we're likely within the margins of error @@ -139,7 +139,7 @@ function createSuite(suiteConfig) { return suite.on('cycle', function(event) { var mean = event.target.stats.mean * 1000; - console.log(event.target + ", " + mean.toFixed(1) + "ms per sample"); + console.log(event.target + ", " + (+mean.toPrecision(2)) + "ms per run"); var version = event.target.options.versionName; totalTime[version] += mean; }) diff --git a/perf/suites.js b/perf/suites.js index 6b00b2535..9a36db53b 100644 --- a/perf/suites.js +++ b/perf/suites.js @@ -7,7 +7,7 @@ module.exports = [ // args lists are passed to the setup function args: [[10], [300], [10000]], setup: function(count) { - tasks = Array(count); + tasks = _.range(count); }, fn: function (async, done) { async.each(tasks, function (num, cb) { @@ -19,7 +19,7 @@ module.exports = [ name: "eachSeries", args: [[10], [300], [10000]], setup: function(count) { - tasks = Array(count); + tasks = _.range(count); }, fn: function (async, done) { async.eachSeries(tasks, function (num, cb) { @@ -31,7 +31,7 @@ module.exports = [ name: "eachLimit", args: [[10], [300], [10000]], setup: function(count) { - tasks = Array(count); + tasks = _.range(count); }, fn: function (async, done) { async.eachLimit(tasks, 4, function (num, cb) { @@ -44,10 +44,10 @@ module.exports = [ // args lists are passed to the setup function args: [[10], [300], [10000]], setup: function(count) { - tasks = Array(count); + tasks = _.range(count); }, fn: function (async, done) { - async.map(Array(10), function (num, cb) { + async.map(tasks, function (num, cb) { async.setImmediate(cb); }, done); } @@ -56,7 +56,7 @@ module.exports = [ name: "mapSeries", args: [[10], [300], [10000]], setup: function(count) { - tasks = Array(count); + tasks = _.range(count); }, fn: function (async, done) { async.mapSeries(tasks, function (num, cb) { @@ -68,7 +68,7 @@ module.exports = [ name: "mapLimit", args: [[10], [300], [10000]], setup: function(count) { - tasks = Array(count); + tasks = _.range(count); }, fn: function (async, done) { async.mapLimit(tasks, 4, function (num, cb) { @@ -118,7 +118,9 @@ module.exports = [ args: [[10], [100], [1000]], setup: function (count) { tasks = _.range(count).map(function () { - return function (cb) { cb(); }; + return function (cb) { + setImmediate(cb); + }; }); }, fn: function (async, done) { @@ -130,7 +132,7 @@ module.exports = [ args: [[10], [100], [1000]], setup: function (count) { tasks = _.range(count).map(function () { - return function (cb) { cb(); }; + return function (cb) { setImmediate(cb); }; }); }, fn: function (async, done) { From 8f5dbd6ce32d4301addb8f81ee623aac17e19f6a Mon Sep 17 00:00:00 2001 From: Bao Date: Fri, 22 May 2015 01:20:04 -0700 Subject: [PATCH 101/956] Added test for _eachLimit stopping replenishing after error --- test/test-async.js | 83 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/test/test-async.js b/test/test-async.js index 1cb860ebc..81c178700 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -1007,6 +1007,42 @@ exports['parallel call in another context'] = function(test) { vm.runInNewContext(fn, sandbox); }; +exports['parallel does not continue replenishing after error'] = function (test) { + var started = 0; + var arr = [ + funcToCall, + funcToCall, + funcToCall, + funcToCall, + funcToCall, + funcToCall, + funcToCall, + funcToCall, + funcToCall, + ]; + var delay = 10; + var limit = 3; + var maxTime = 10 * arr.length; + function funcToCall(callback) { + started ++; + if (started === 3) { + return callback(new Error ("Test Error")); + } + setTimeout(function(){ + callback(); + }, delay); + } + + async.parallelLimit(arr, limit, function(x, callback) { + + }, function(err){}); + + setTimeout(function(){ + test.equal(started, 3); + test.done(); + }, maxTime); +}; + exports['series'] = function(test){ var call_order = []; @@ -1386,6 +1422,30 @@ exports['eachLimit synchronous'] = function(test){ }); }; + +exports['eachLimit does not continue replenishing after error'] = function (test) { + var started = 0; + var arr = [0,1,2,3,4,5,6,7,8,9]; + var delay = 10; + var limit = 3; + var maxTime = 10 * arr.length; + + async.eachLimit(arr, limit, function(x, callback) { + started ++; + if (started === 3) { + return callback(new Error ("Test Error")); + } + setTimeout(function(){ + callback(); + }, delay); + }, function(err){}); + + setTimeout(function(){ + test.equal(started, 3); + test.done(); + }, maxTime); +}; + exports['forEachSeries alias'] = function (test) { test.strictEqual(async.eachSeries, async.forEachSeries); test.done(); @@ -1668,6 +1728,29 @@ exports['mapLimit error'] = function(test){ setTimeout(test.done, 25); }; +exports['mapLimit does not continue replenishing after error'] = function (test) { + var started = 0; + var arr = [0,1,2,3,4,5,6,7,8,9]; + var delay = 10; + var limit = 3; + var maxTime = 10 * arr.length; + + async.mapLimit(arr, limit, function(x, callback) { + started ++; + if (started === 3) { + return callback(new Error ("Test Error")); + } + setTimeout(function(){ + callback(); + }, delay); + }, function(err){}); + + setTimeout(function(){ + test.equal(started, 3); + test.done(); + }, maxTime); +}; + exports['reduce'] = function(test){ var call_order = []; From 8af90e31692db66058df0acb8831922b8a6b9a19 Mon Sep 17 00:00:00 2001 From: majecty Date: Fri, 22 May 2015 21:43:13 +0900 Subject: [PATCH 102/956] Update README.md Fix document errors about 'times' and 'timesSeries'. --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4e0b9fca1..6775ad541 100644 --- a/README.md +++ b/README.md @@ -1573,9 +1573,9 @@ call_order.push('one') ``` -### times(n, callback) +### times(n, iterator, callback) -Calls the `callback` function `n` times, and accumulates results in the same manner +Calls the `iterator` function `n` times, and accumulates results in the same manner you would use with [`map`](#map). __Arguments__ @@ -1604,7 +1604,7 @@ async.times(5, function(n, next){ ``` -### timesSeries(n, callback) +### timesSeries(n, iterator, callback) The same as [`times`](#times), only the iterator is applied to each item in `arr` in series. The next `iterator` is only called once the current one has completed. From 4a4247479ec88ec14a6a5313daee3f558c36a63d Mon Sep 17 00:00:00 2001 From: Benjamin Coe Date: Sat, 23 May 2015 15:33:41 -0700 Subject: [PATCH 103/956] added test coverage with coveralls and nyc --- .gitignore | 2 + .travis.yml | 1 + README.md | 160 +++++++++++++++++++++++++-------------------------- package.json | 6 +- 4 files changed, 88 insertions(+), 81 deletions(-) diff --git a/.gitignore b/.gitignore index 291d97eb0..82b59efd7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ node_modules dist perf/versions +nyc_output +coverage diff --git a/.travis.yml b/.travis.yml index 6064ca092..eb35ea044 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,3 +3,4 @@ node_js: - "0.10" - "0.12" - "iojs" +after_success: npm run coveralls diff --git a/README.md b/README.md index 6775ad541..2257d9bd9 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![Build Status via Travis CI](https://travis-ci.org/caolan/async.svg?branch=master)](https://travis-ci.org/caolan/async) [![NPM version](http://img.shields.io/npm/v/async.svg)](https://www.npmjs.org/package/async) - +[![Coverage Status](https://coveralls.io/repos/bcoe/async/badge.svg?branch=)](https://coveralls.io/r/bcoe/async?branch=) Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript. Although originally designed for @@ -62,7 +62,7 @@ a method of another library isn't working as an iterator, study this example: // Here is a simple object with an (unnecessarily roundabout) squaring method var AsyncSquaringLibrary = { squareExponent: 2, - square: function(number, callback){ + square: function(number, callback){ var result = Math.pow(number, this.squareExponent); setTimeout(function(){ callback(null, result); @@ -80,7 +80,7 @@ async.map([1, 2, 3], AsyncSquaringLibrary.square, function(err, result){ async.map([1, 2, 3], AsyncSquaringLibrary.square.bind(AsyncSquaringLibrary), function(err, result){ // result is [1, 4, 9] // With the help of bind we can attach a context to the iterator before - // passing it to async. Now the square function will be executed in its + // passing it to async. Now the square function will be executed in its // 'home' AsyncSquaringLibrary context and the value of `this.squareExponent` // will be as expected. }); @@ -94,7 +94,7 @@ Alternatively, you can install using Node Package Manager (`npm`): npm install async -As well as using Bower: +As well as using Bower: bower install async @@ -102,7 +102,7 @@ __Development:__ [async.js](https://github.com/caolan/async/raw/master/lib/async ## In the Browser -So far it's been tested in IE6, IE7, IE8, FF3.6 and Chrome 5. +So far it's been tested in IE6, IE7, IE8, FF3.6 and Chrome 5. Usage: @@ -197,8 +197,8 @@ __Arguments__ * `arr` - An array to iterate over. * `iterator(item, callback)` - A function to apply to each item in `arr`. - The iterator is passed a `callback(err)` which must be called once it has - completed. If no error has occurred, the `callback` should be run without + The iterator is passed a `callback(err)` which must be called once it has + completed. If no error has occurred, the `callback` should be run without arguments or with an explicit `null` argument. The array index is not passed to the iterator. If you need the index, use [`forEachOf`](#forEachOf). * `callback(err)` - A callback which is called when all `iterator` functions @@ -217,13 +217,13 @@ async.each(openFiles, saveFile, function(err){ ``` ```js -// assuming openFiles is an array of file names +// assuming openFiles is an array of file names async.each(openFiles, function(file, callback) { - + // Perform operation on file here. console.log('Processing file ' + file); - + if( file.length > 32 ) { console.log('This file name is too long'); callback('File name too long'); @@ -251,7 +251,7 @@ async.each(openFiles, function(file, callback) { ### eachSeries(arr, iterator, callback) The same as [`each`](#each), only `iterator` is applied to each item in `arr` in -series. The next `iterator` is only called once the current one has completed. +series. The next `iterator` is only called once the current one has completed. This means the `iterator` functions will complete in order. @@ -261,10 +261,10 @@ This means the `iterator` functions will complete in order. ### eachLimit(arr, limit, iterator, callback) -The same as [`each`](#each), only no more than `limit` `iterator`s will be simultaneously +The same as [`each`](#each), only no more than `limit` `iterator`s will be simultaneously running at any time. -Note that the items in `arr` are not processed in batches, so there is no guarantee that +Note that the items in `arr` are not processed in batches, so there is no guarantee that the first `limit` `iterator` functions will complete before any others are started. __Arguments__ @@ -272,8 +272,8 @@ __Arguments__ * `arr` - An array to iterate over. * `limit` - The maximum number of `iterator`s to run at any time. * `iterator(item, callback)` - A function to apply to each item in `arr`. - The iterator is passed a `callback(err)` which must be called once it has - completed. If no error has occurred, the callback should be run without + The iterator is passed a `callback(err)` which must be called once it has + completed. If no error has occurred, the callback should be run without arguments or with an explicit `null` argument. * `callback(err)` - A callback which is called when all `iterator` functions have finished, or an error occurs. @@ -301,8 +301,8 @@ Like `each`, except that it iterates over objects, and passes the key as the sec __Arguments__ * `obj` - An object or array to iterate over. -* `iterator(item, key, callback)` - A function to apply to each item in `obj`. -The `key` is the item's key, or index in the case of an array. The iterator is +* `iterator(item, key, callback)` - A function to apply to each item in `obj`. +The `key` is the item's key, or index in the case of an array. The iterator is passed a `callback(err)` which must be called once it has completed. If no error has occurred, the callback should be run without arguments or with an explicit `null` argument. @@ -357,19 +357,19 @@ Like [`forEachOf`](#forEachOf), except the number of `iterator`s running at a gi Produces a new array of values by mapping each value in `arr` through the `iterator` function. The `iterator` is called with an item from `arr` and a -callback for when it has finished processing. Each of these callback takes 2 arguments: -an `error`, and the transformed item from `arr`. If `iterator` passes an error to its +callback for when it has finished processing. Each of these callback takes 2 arguments: +an `error`, and the transformed item from `arr`. If `iterator` passes an error to its callback, the main `callback` (for the `map` function) is immediately called with the error. Note, that since this function applies the `iterator` to each item in parallel, -there is no guarantee that the `iterator` functions will complete in order. +there is no guarantee that the `iterator` functions will complete in order. However, the results array will be in the same order as the original `arr`. __Arguments__ * `arr` - An array to iterate over. * `iterator(item, callback)` - A function to apply to each item in `arr`. - The iterator is passed a `callback(err, transformed)` which must be called once + The iterator is passed a `callback(err, transformed)` which must be called once it has completed with an error (which can be `null`) and a transformed item. * `callback(err, results)` - A callback which is called when all `iterator` functions have finished, or an error occurs. Results is an array of the @@ -389,7 +389,7 @@ async.map(['file1','file2','file3'], fs.stat, function(err, results){ ### mapSeries(arr, iterator, callback) The same as [`map`](#map), only the `iterator` is applied to each item in `arr` in -series. The next `iterator` is only called once the current one has completed. +series. The next `iterator` is only called once the current one has completed. The results array will be in the same order as the original. @@ -398,10 +398,10 @@ The results array will be in the same order as the original. ### mapLimit(arr, limit, iterator, callback) -The same as [`map`](#map), only no more than `limit` `iterator`s will be simultaneously +The same as [`map`](#map), only no more than `limit` `iterator`s will be simultaneously running at any time. -Note that the items are not processed in batches, so there is no guarantee that +Note that the items are not processed in batches, so there is no guarantee that the first `limit` `iterator` functions will complete before any others are started. __Arguments__ @@ -409,7 +409,7 @@ __Arguments__ * `arr` - An array to iterate over. * `limit` - The maximum number of `iterator`s to run at any time. * `iterator(item, callback)` - A function to apply to each item in `arr`. - The iterator is passed a `callback(err, transformed)` which must be called once + The iterator is passed a `callback(err, transformed)` which must be called once it has completed with an error (which can be `null`) and a transformed item. * `callback(err, results)` - A callback which is called when all `iterator` calls have finished, or an error occurs. The result is an array of the @@ -442,7 +442,7 @@ __Arguments__ * `arr` - An array to iterate over. * `iterator(item, callback)` - A truth test to apply to each item in `arr`. - The `iterator` is passed a `callback(truthValue)`, which must be called with a + The `iterator` is passed a `callback(truthValue)`, which must be called with a boolean argument once it has completed. * `callback(results)` - A callback which is called after all the `iterator` functions have finished. @@ -464,7 +464,7 @@ async.filter(['file1','file2','file3'], fs.exists, function(results){ __Alias:__ `selectSeries` The same as [`filter`](#filter) only the `iterator` is applied to each item in `arr` in -series. The next `iterator` is only called once the current one has completed. +series. The next `iterator` is only called once the current one has completed. The results array will be in the same order as the original. --------------------------------------- @@ -491,12 +491,12 @@ in series. __Aliases:__ `inject`, `foldl` Reduces `arr` into a single value using an async `iterator` to return -each successive step. `memo` is the initial state of the reduction. -This function only operates in series. +each successive step. `memo` is the initial state of the reduction. +This function only operates in series. -For performance reasons, it may make sense to split a call to this function into -a parallel map, and then use the normal `Array.prototype.reduce` on the results. -This function is for situations where each step in the reduction needs to be async; +For performance reasons, it may make sense to split a call to this function into +a parallel map, and then use the normal `Array.prototype.reduce` on the results. +This function is for situations where each step in the reduction needs to be async; if you can get the data before reducing it, then it's probably a good idea to do so. __Arguments__ @@ -505,9 +505,9 @@ __Arguments__ * `memo` - The initial state of the reduction. * `iterator(memo, item, callback)` - A function applied to each item in the array to produce the next step in the reduction. The `iterator` is passed a - `callback(err, reduction)` which accepts an optional error as its first - argument, and the state of the reduction as the second. If an error is - passed to the callback, the reduction is stopped and the main `callback` is + `callback(err, reduction)` which accepts an optional error as its first + argument, and the state of the reduction as the second. If an error is + passed to the callback, the reduction is stopped and the main `callback` is immediately called with the error. * `callback(err, result)` - A callback which is called after all the `iterator` functions have finished. Result is the reduced value. @@ -551,7 +551,7 @@ __Arguments__ * `arr` - An array to iterate over. * `iterator(item, callback)` - A truth test to apply to each item in `arr`. - The iterator is passed a `callback(truthValue)` which must be called with a + The iterator is passed a `callback(truthValue)` which must be called with a boolean argument once it has completed. **Note: this callback does not take an error as its first argument.** * `callback(result)` - A callback which is called as soon as any iterator returns `true`, or after all the `iterator` functions have finished. Result will be @@ -644,7 +644,7 @@ __Arguments__ * `arr` - An array to iterate over. * `iterator(item, callback)` - A truth test to apply to each item in the array - in parallel. The iterator is passed a `callback(truthValue)`` which must be + in parallel. The iterator is passed a `callback(truthValue)`` which must be called with a boolean argument once it has completed. * `callback(result)` - A callback which is called as soon as any iterator returns `true`, or after all the iterator functions have finished. Result will be @@ -675,7 +675,7 @@ __Arguments__ * `arr` - An array to iterate over. * `iterator(item, callback)` - A truth test to apply to each item in the array - in parallel. The iterator is passed a `callback(truthValue)` which must be + in parallel. The iterator is passed a `callback(truthValue)` which must be called with a boolean argument once it has completed. * `callback(result)` - A callback which is called after all the `iterator` functions have finished. Result will be either `true` or `false` depending on @@ -705,7 +705,7 @@ __Arguments__ * `arr` - An array to iterate over. * `iterator(item, callback)` - A function to apply to each item in `arr`. - The iterator is passed a `callback(err, results)` which must be called once it + The iterator is passed a `callback(err, results)` which must be called once it has completed with an error (which can be `null`) and an array of results. * `callback(err, results)` - A callback which is called after all the `iterator` functions have finished, or an error occurs. Results is an array containing @@ -734,7 +734,7 @@ Same as [`concat`](#concat), but executes in series instead of parallel. Run the functions in the `tasks` array in series, each one running once the previous function has completed. If any functions in the series pass an error to its -callback, no more functions are run, and `callback` is immediately called with the value of the error. +callback, no more functions are run, and `callback` is immediately called with the value of the error. Otherwise, `callback` receives an array of results when `tasks` have completed. It is also possible to use an object instead of an array. Each property will be @@ -743,13 +743,13 @@ instead of an array. This can be a more readable way of handling results from [`series`](#series). **Note** that while many implementations preserve the order of object properties, the -[ECMAScript Language Specifcation](http://www.ecma-international.org/ecma-262/5.1/#sec-8.6) +[ECMAScript Language Specifcation](http://www.ecma-international.org/ecma-262/5.1/#sec-8.6) explicitly states that > The mechanics and order of enumerating the properties is not specified. So if you rely on the order in which your series of functions are executed, and want -this to work on all platforms, consider using an array. +this to work on all platforms, consider using an array. __Arguments__ @@ -757,7 +757,7 @@ __Arguments__ a `callback(err, result)` it must call on completion with an error `err` (which can be `null`) and an optional `result` value. * `callback(err, results)` - An optional callback to run once all the functions - have completed. This function gets a results array (or object) containing all + have completed. This function gets a results array (or object) containing all the result arguments passed to the `task` callbacks. __Example__ @@ -816,11 +816,11 @@ instead of an array. This can be a more readable way of handling results from __Arguments__ -* `tasks` - An array or object containing functions to run. Each function is passed - a `callback(err, result)` which it must call on completion with an error `err` +* `tasks` - An array or object containing functions to run. Each function is passed + a `callback(err, result)` which it must call on completion with an error `err` (which can be `null`) and an optional `result` value. * `callback(err, results)` - An optional callback to run once all the functions - have completed. This function gets a results array (or object) containing all + have completed. This function gets a results array (or object) containing all the result arguments passed to the task callbacks. __Example__ @@ -868,20 +868,20 @@ function(err, results) { ### parallelLimit(tasks, limit, [callback]) -The same as [`parallel`](#parallel), only `tasks` are executed in parallel +The same as [`parallel`](#parallel), only `tasks` are executed in parallel with a maximum of `limit` tasks executing at any time. -Note that the `tasks` are not executed in batches, so there is no guarantee that +Note that the `tasks` are not executed in batches, so there is no guarantee that the first `limit` tasks will complete before any others are started. __Arguments__ -* `tasks` - An array or object containing functions to run, each function is passed +* `tasks` - An array or object containing functions to run, each function is passed a `callback(err, result)` it must call on completion with an error `err` (which can be `null`) and an optional `result` value. * `limit` - The maximum number of `tasks` to run at any time. * `callback(err, results)` - An optional callback to run once all the functions - have completed. This function gets a results array (or object) containing all + have completed. This function gets a results array (or object) containing all the result arguments passed to the `task` callbacks. --------------------------------------- @@ -896,7 +896,7 @@ __Arguments__ * `test()` - synchronous truth test to perform before each execution of `fn`. * `fn(callback)` - A function which is called each time `test` passes. The function is - passed a `callback(err)`, which must be called once it has completed with an + passed a `callback(err)`, which must be called once it has completed with an optional `err` argument. * `callback(err)` - A callback which is called after the test fails and repeated execution of `fn` has stopped. @@ -923,8 +923,8 @@ async.whilst( ### doWhilst(fn, test, callback) -The post-check version of [`whilst`](#whilst). To reflect the difference in -the order of operations, the arguments `test` and `fn` are switched. +The post-check version of [`whilst`](#whilst). To reflect the difference in +the order of operations, the arguments `test` and `fn` are switched. `doWhilst` is to `whilst` as `do while` is to `while` in plain JavaScript. @@ -981,9 +981,9 @@ the error. __Arguments__ -* `tasks` - An array of functions to run, each function is passed a +* `tasks` - An array of functions to run, each function is passed a `callback(err, result1, result2, ...)` it must call on completion. The first - argument is an error (which can be `null`) and any further arguments will be + argument is an error (which can be `null`) and any further arguments will be passed as arguments in order to the next task. * `callback(err, [results])` - An optional callback to run once all the functions have completed. This will be passed the results of the last task's callback. @@ -1006,7 +1006,7 @@ async.waterfall([ callback(null, 'done'); } ], function (err, result) { - // result now equals 'done' + // result now equals 'done' }); ``` @@ -1068,7 +1068,7 @@ __Example__ ```js // Requires lodash (or underscore), express3 and dresende's orm2. // Part of an app, that fetches cats of the logged user. -// This example uses `seq` function to avoid overnesting and error +// This example uses `seq` function to avoid overnesting and error // handling clutter. app.get('/cats', function(request, response) { var User = request.models.User; @@ -1092,7 +1092,7 @@ app.get('/cats', function(request, response) { ### applyEach(fns, args..., callback) -Applies the provided arguments to each function in the array, calling +Applies the provided arguments to each function in the array, calling `callback` after all functions have completed. If you only provide the first argument, then it will return a function which lets you pass in the arguments as if it were a single function call. @@ -1132,13 +1132,13 @@ The same as [`applyEach`](#applyEach) only the functions are applied in series. Creates a `queue` object with the specified `concurrency`. Tasks added to the `queue` are processed in parallel (up to the `concurrency` limit). If all -`worker`s are in progress, the task is queued until one becomes available. +`worker`s are in progress, the task is queued until one becomes available. Once a `worker` completes a `task`, that `task`'s callback is called. __Arguments__ * `worker(task, callback)` - An asynchronous function for processing a queued - task, which must call its `callback(err)` argument when finished, with an + task, which must call its `callback(err)` argument when finished, with an optional `error` as an argument. If you want to handle errors from an individual task, pass a callback to `q.push()`. * `concurrency` - An `integer` for determining how many `worker` functions should be run in parallel. If omitted, the concurrency defaults to `1`. If the concurrency is `0`, an error is thrown. @@ -1155,11 +1155,11 @@ methods: * `concurrency` - an integer for determining how many `worker` functions should be run in parallel. This property can be changed after a `queue` is created to alter the concurrency on-the-fly. -* `push(task, [callback])` - add a new task to the `queue`. Calls `callback` once +* `push(task, [callback])` - add a new task to the `queue`. Calls `callback` once the `worker` has finished processing the task. Instead of a single task, a `tasks` array can be submitted. The respective callback is used for every task in the list. * `unshift(task, [callback])` - add a new task to the front of the `queue`. -* `saturated` - a callback that is called when the `queue` length hits the `concurrency` limit, +* `saturated` - a callback that is called when the `queue` length hits the `concurrency` limit, and further tasks will be queued. * `empty` - a callback that is called when the last item from the `queue` is given to a `worker`. * `drain` - a callback that is called when the last item from the `queue` has returned from the `worker`. @@ -1236,7 +1236,7 @@ when the worker is finished. __Arguments__ * `worker(tasks, callback)` - An asynchronous function for processing an array of - queued tasks, which must call its `callback(err)` argument when finished, with + queued tasks, which must call its `callback(err)` argument when finished, with an optional `err` argument. * `payload` - An optional `integer` for determining how many tasks should be processed per round; if omitted, the default is unlimited. @@ -1251,7 +1251,7 @@ methods: process per round. This property can be changed after a `cargo` is created to alter the payload on-the-fly. * `push(task, [callback])` - Adds `task` to the `queue`. The callback is called - once the `worker` has finished processing the task. Instead of a single task, an array of `tasks` + once the `worker` has finished processing the task. Instead of a single task, an array of `tasks` can be submitted. The respective callback is used for every task in the list. * `saturated` - A callback that is called when the `queue.length()` hits the concurrency and further tasks will be queued. * `empty` - A callback that is called when the last item from the `queue` is given to a `worker`. @@ -1288,18 +1288,18 @@ cargo.push({name: 'baz'}, function (err) { ### auto(tasks, [callback]) -Determines the best order for running the functions in `tasks`, based on their -requirements. Each function can optionally depend on other functions being completed -first, and each function is run as soon as its requirements are satisfied. +Determines the best order for running the functions in `tasks`, based on their +requirements. Each function can optionally depend on other functions being completed +first, and each function is run as soon as its requirements are satisfied. -If any of the functions pass an error to their callback, it will not -complete (so any other functions depending on it will not run), and the main -`callback` is immediately called with the error. Functions also receive an +If any of the functions pass an error to their callback, it will not +complete (so any other functions depending on it will not run), and the main +`callback` is immediately called with the error. Functions also receive an object containing the results of functions which have completed so far. -Note, all functions are called with a `results` object as a second argument, +Note, all functions are called with a `results` object as a second argument, so it is unsafe to pass functions in the `tasks` object which cannot handle the -extra argument. +extra argument. For example, this snippet of code: @@ -1316,7 +1316,7 @@ argument, which will fail: fs.readFile('data.txt', 'utf-8', cb, {}); ``` -Instead, wrap the call to `readFile` in a function which does not forward the +Instead, wrap the call to `readFile` in a function which does not forward the `results` object: ```js @@ -1333,13 +1333,13 @@ __Arguments__ requirements, with the function itself the last item in the array. The object's key of a property serves as the name of the task defined by that property, i.e. can be used when specifying requirements for other tasks. - The function receives two arguments: (1) a `callback(err, result)` which must be - called when finished, passing an `error` (which can be `null`) and the result of + The function receives two arguments: (1) a `callback(err, result)` which must be + called when finished, passing an `error` (which can be `null`) and the result of the function's execution, and (2) a `results` object, containing the results of the previously executed functions. * `callback(err, results)` - An optional callback which is called when all the - tasks have been completed. It receives the `err` argument if any `tasks` - pass an error to their callback. Results are always returned; however, if + tasks have been completed. It receives the `err` argument if any `tasks` + pass an error to their callback. Results are always returned; however, if an error occurs, no further `tasks` will be performed, and the results object will only contain partial results. @@ -1430,7 +1430,7 @@ __Arguments__ * `times` - An integer indicating how many times to attempt the `task` before giving up. Defaults to 5. * `task(callback, results)` - A function which receives two arguments: (1) a `callback(err, result)` - which must be called when finished, passing `err` (which can be `null`) and the `result` of + which must be called when finished, passing `err` (which can be `null`) and the `result` of the function's execution, and (2) a `results` object, containing the results of the previously executed functions (if nested inside another control flow). * `callback(err, results)` - An optional callback which is called when the @@ -1499,7 +1499,7 @@ node> nextfn(); ### apply(function, arguments..) -Creates a continuation function with some arguments already applied. +Creates a continuation function with some arguments already applied. Useful as a shorthand when combined with other control flow functions. Any arguments passed to the returned function are added to the arguments originally passed @@ -1607,7 +1607,7 @@ async.times(5, function(n, next){ ### timesSeries(n, iterator, callback) The same as [`times`](#times), only the iterator is applied to each item in `arr` in -series. The next `iterator` is only called once the current one has completed. +series. The next `iterator` is only called once the current one has completed. The results array will be in the same order as the original. diff --git a/package.json b/package.json index 6e7282be7..601f50f5f 100644 --- a/package.json +++ b/package.json @@ -20,10 +20,12 @@ "license": "MIT", "devDependencies": { "benchmark": "bestiejs/benchmark.js", + "coveralls": "^2.11.2", "jshint": "~2.7.0", "lodash": ">=2.4.1", "mkdirp": "~0.5.1", "nodeunit": ">0.0.0", + "nyc": "^2.1.0", "uglify-js": "1.2.x", "yargs": "~3.9.1" }, @@ -40,7 +42,9 @@ }, "scripts": { "test": "npm run-script lint && nodeunit test/test-async.js", - "lint": "jshint lib/*.js test/*.js perf/*.js" + "lint": "jshint lib/*.js test/*.js perf/*.js", + "coverage": "nyc npm test && nyc report", + "coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls" }, "spm": { "main": "lib/async.js" From d5e669f64cb16eeb46f988885d94ccd88ab4dd4d Mon Sep 17 00:00:00 2001 From: Benjamin Coe Date: Sat, 23 May 2015 16:41:11 -0700 Subject: [PATCH 104/956] switch to caolan from bcoe --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2257d9bd9..d09f9f0b7 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![Build Status via Travis CI](https://travis-ci.org/caolan/async.svg?branch=master)](https://travis-ci.org/caolan/async) [![NPM version](http://img.shields.io/npm/v/async.svg)](https://www.npmjs.org/package/async) -[![Coverage Status](https://coveralls.io/repos/bcoe/async/badge.svg?branch=)](https://coveralls.io/r/bcoe/async?branch=) +[![Coverage Status](https://coveralls.io/repos/caolan/async/badge.svg?branch=)](https://coveralls.io/r/caolan/async?branch=) Async is a utility module which provides straight-forward, powerful functions for working with asynchronous JavaScript. Although originally designed for From 7447767c73c8f6aed83c6fb3feee24996b3bf2e9 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Sun, 24 May 2015 23:58:28 -0700 Subject: [PATCH 105/956] initial ensureAsync implementation --- lib/async.js | 22 +++++++++++++++++ perf/benchmark.js | 6 ++++- perf/suites.js | 24 +++++++++++++++++++ test/test-async.js | 60 ++++++++++++++++++++++++++++++++++++++++++---- 4 files changed, 107 insertions(+), 5 deletions(-) diff --git a/lib/async.js b/lib/async.js index 4257f0de5..86c810a3a 100644 --- a/lib/async.js +++ b/lib/async.js @@ -1282,4 +1282,26 @@ root.async = async; } + function ensureAsync(fn) { + return function (/*...args, callback*/) { + var args = _baseSlice(arguments); + var callback = args.pop(); + args.push(function () { + var innerArgs = arguments; + if (sync) { + async.setImmediate(function () { + callback.apply(null, innerArgs); + }); + } else { + callback.apply(null, innerArgs); + } + }); + var sync = true; + fn.apply(this, args); + sync = false; + }; + } + + async.ensureAsync = ensureAsync; + }()); diff --git a/perf/benchmark.js b/perf/benchmark.js index da5921647..9e57fd961 100755 --- a/perf/benchmark.js +++ b/perf/benchmark.js @@ -129,7 +129,10 @@ function createSuite(suiteConfig) { }); }, _.extend({ versionName: versionName, - setup: _.partial.apply(null, [suiteConfig.setup].concat(args)) + setup: _.partial.apply(null, [suiteConfig.setup].concat(args)), + onError: function (err) { + console.log(err.stack); + } }, benchOptions)); } @@ -143,6 +146,7 @@ function createSuite(suiteConfig) { var version = event.target.options.versionName; totalTime[version] += mean; }) + .on('error', function (err) { console.error(err); }) .on('complete', function() { var fastest = this.filter('fastest'); if (fastest.length === 2) { diff --git a/perf/suites.js b/perf/suites.js index 9a36db53b..28b5a3236 100644 --- a/perf/suites.js +++ b/perf/suites.js @@ -204,6 +204,30 @@ module.exports = [ fn: function (async, done) { setTimeout(done, 0); } + }, + { + name: "ensureAsync sync", + fn: function (async, done) { + async.ensureAsync(function (cb) { + cb(); + })(done); + } + }, + { + name: "ensureAsync async", + fn: function (async, done) { + async.ensureAsync(function (cb) { + setImmediate(cb); + })(done); + } + }, + { + name: "ensureAsync async noWrap", + fn: function (async, done) { + (function (cb) { + setImmediate(cb); + }(done)); + } } ]; diff --git a/test/test-async.js b/test/test-async.js index 81c178700..6797afeff 100755 --- a/test/test-async.js +++ b/test/test-async.js @@ -1030,12 +1030,12 @@ exports['parallel does not continue replenishing after error'] = function (test) } setTimeout(function(){ callback(); - }, delay); + }, delay); } async.parallelLimit(arr, limit, function(x, callback) { - }, function(err){}); + }, function(err){}); setTimeout(function(){ test.equal(started, 3); @@ -1438,7 +1438,7 @@ exports['eachLimit does not continue replenishing after error'] = function (test setTimeout(function(){ callback(); }, delay); - }, function(err){}); + }, function(err){}); setTimeout(function(){ test.equal(started, 3); @@ -1743,7 +1743,7 @@ exports['mapLimit does not continue replenishing after error'] = function (test) setTimeout(function(){ callback(); }, delay); - }, function(err){}); + }, function(err){}); setTimeout(function(){ test.equal(started, 3); @@ -3561,3 +3561,55 @@ exports['queue started'] = function(test) { }; +exports['ensureAsync'] = { + 'defer sync functions': function (test) { + var sync = true; + async.ensureAsync(function (arg1, arg2, cb) { + test.equal(arg1, 1); + test.equal(arg2, 2); + cb(null, 4, 5); + })(1, 2, function (err, arg4, arg5) { + test.equal(err, null); + test.equal(arg4, 4); + test.equal(arg5, 5); + test.ok(!sync, 'callback called on same tick'); + test.done(); + }); + sync = false; + }, + + 'do not defer async functions': function (test) { + var sync = false; + async.ensureAsync(function (arg1, arg2, cb) { + test.equal(arg1, 1); + test.equal(arg2, 2); + async.setImmediate(function () { + sync = true; + cb(null, 4, 5); + sync = false; + }); + })(1, 2, function (err, arg4, arg5) { + test.equal(err, null); + test.equal(arg4, 4); + test.equal(arg5, 5); + test.ok(sync, 'callback called on next tick'); + test.done(); + }); + }, + + 'double wrapping': function (test) { + var sync = true; + async.ensureAsync(async.ensureAsync(function (arg1, arg2, cb) { + test.equal(arg1, 1); + test.equal(arg2, 2); + cb(null, 4, 5); + }))(1, 2, function (err, arg4, arg5) { + test.equal(err, null); + test.equal(arg4, 4); + test.equal(arg5, 5); + test.ok(!sync, 'callback called on same tick'); + test.done(); + }); + sync = false; + } +}; From 219b4fbc93cb3c8c0a1bbdd7e58028d6087d1c73 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Mon, 25 May 2015 14:40:17 -0700 Subject: [PATCH 106/956] handle errors in benchmarks --- perf/benchmark.js | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/perf/benchmark.js b/perf/benchmark.js index 9e57fd961..5cd871b87 100755 --- a/perf/benchmark.js +++ b/perf/benchmark.js @@ -2,7 +2,6 @@ var _ = require("lodash"); var Benchmark = require("benchmark"); -var benchOptions = {defer: true, minSamples: 1, maxTime: 2}; var exec = require("child_process").exec; var fs = require("fs"); var path = require("path"); @@ -16,8 +15,11 @@ var args = require("yargs") .alias("g", "grep") .default("g", ".*") .describe("i", "skip benchmarks whose names match this regex") - .alias("g", "reject") + .alias("i", "reject") .default("i", "^$") + .describe("l", "maximum running time per test (in seconds)") + .alias("l", "limit") + .default("l", 2) .help('h') .alias('h', 'help') .example('$0 0.9.2 0.9.0', 'Compare v0.9.2 with v0.9.0') @@ -33,6 +35,7 @@ var reject = new RegExp(args.i, "i"); var version0 = args._[0] || require("../package.json").version; var version1 = args._[1] || "current"; var versionNames = [version0, version1]; +var benchOptions = {defer: true, minSamples: 1, maxTime: +args.l}; var versions; var wins = {}; var totalTime = {}; @@ -120,9 +123,20 @@ function doesNotMatch(suiteConfig) { function createSuite(suiteConfig) { var suite = new Benchmark.Suite(); var args = suiteConfig.args; + var errored = false; function addBench(version, versionName) { var name = suiteConfig.name + " " + versionName; + + try { + suiteConfig.setup(1); + suiteConfig.fn(version, function () {}); + } catch (e) { + console.error(name + " Errored"); + errored = true; + return; + } + suite.add(name, function (deferred) { suiteConfig.fn(version, function () { deferred.resolve(); @@ -142,19 +156,22 @@ function createSuite(suiteConfig) { return suite.on('cycle', function(event) { var mean = event.target.stats.mean * 1000; - console.log(event.target + ", " + (+mean.toPrecision(2)) + "ms per run"); + console.log(event.target + ", " + (+mean.toPrecision(3)) + "ms per run"); var version = event.target.options.versionName; + if (errored) return; totalTime[version] += mean; }) .on('error', function (err) { console.error(err); }) .on('complete', function() { - var fastest = this.filter('fastest'); - if (fastest.length === 2) { - console.log("Tie"); - } else { - var winner = fastest[0].options.versionName; - console.log(winner + ' is faster'); - wins[winner]++; + if (!errored) { + var fastest = this.filter('fastest'); + if (fastest.length === 2) { + console.log("Tie"); + } else { + var winner = fastest[0].options.versionName; + console.log(winner + ' is faster'); + wins[winner]++; + } } console.log("--------------------------------------"); }); From aba3e0c1af87f0f89bbddb6d3a2c56ffb9176c9b Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Mon, 25 May 2015 14:58:27 -0700 Subject: [PATCH 107/956] docs for ensureAsync --- README.md | 36 ++++++++++++++++++++++++++++++++++++ lib/async.js | 30 +++++++++++++++--------------- 2 files changed, 51 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index d09f9f0b7..eadf80158 100644 --- a/README.md +++ b/README.md @@ -174,6 +174,7 @@ Usage: * [`memoize`](#memoize) * [`unmemoize`](#unmemoize) +* [`ensureAsync`](#ensureAsync) * [`log`](#log) * [`dir`](#dir) * [`noConflict`](#noConflict) @@ -1657,6 +1658,41 @@ __Arguments__ * `fn` - the memoized function +--------------------------------------- + + +### ensureAsync(fn) + +Wrap an async function and ensure it calls its callback on a later tick of the event loop. If the function already calls its callback on a next tick, no extra deferral is added. This is useful for preventing stack overflows (`RangeError: Maximum call stack size exceeded`) and generally keeping [Zalgo](http://blog.izs.me/post/59142742143/designing-apis-for-asynchrony) contained. + +__Arguments__ + +* `fn` - an async function, one that expects a node-style callback as its last argument + +Returns a wrapped function with the exact same call signature as the function passed in. + +__Example__ + +```js +function sometimesAsync(arg, callback) { + if (cache[arg]) { + return callback(null, cache[arg]); // this would be synchronous!! + } else { + doSomeIO(arg, callback); // this IO would be asynchronous + } +} + +// this has a risk of stack overflows if many results are cached in a row +async.mapSeries(args, sometimesAsync, done); + +// this will defer sometimesAsync's callback if necessary, +// preventing stack overflows +async.mapSeries(args, async.ensureAsync(sometimesAsync), done); + +``` + +--------------------------------------- + ### log(function, arguments) diff --git a/lib/async.js b/lib/async.js index 86c810a3a..06ede1475 100644 --- a/lib/async.js +++ b/lib/async.js @@ -1267,21 +1267,6 @@ next(); }; - // Node.js - if (typeof module !== 'undefined' && module.exports) { - module.exports = async; - } - // AMD / RequireJS - else if (typeof define !== 'undefined' && define.amd) { - define([], function () { - return async; - }); - } - // included directly via - + + +``` + +## Documentation + +Some functions are also available in the following forms: +* `Series` - the same as `` but runs only a single async operation at a time +* `Limit` - the same as `` but runs a maximum of `limit` async operations at a time + +### Collections + +* [`each`](#each), `eachSeries`, `eachLimit` +* [`forEachOf`](#forEachOf), `forEachOfSeries`, `forEachOfLimit` +* [`map`](#map), `mapSeries`, `mapLimit` +* [`filter`](#filter), `filterSeries`, `filterLimit` +* [`reject`](#reject), `rejectSeries`, `rejectLimit` +* [`reduce`](#reduce), [`reduceRight`](#reduceRight) +* [`detect`](#detect), `detectSeries`, `detectLimit` +* [`sortBy`](#sortBy) +* [`some`](#some), `someLimit` +* [`every`](#every), `everyLimit` +* [`concat`](#concat), `concatSeries` + +### Control Flow + +* [`series`](#seriestasks-callback) +* [`parallel`](#parallel), `parallelLimit` +* [`whilst`](#whilst), [`doWhilst`](#doWhilst) +* [`until`](#until), [`doUntil`](#doUntil) +* [`during`](#during), [`doDuring`](#doDuring) +* [`forever`](#forever) +* [`waterfall`](#waterfall) +* [`compose`](#compose) +* [`seq`](#seq) +* [`applyEach`](#applyEach), `applyEachSeries` +* [`queue`](#queue), [`priorityQueue`](#priorityQueue) +* [`cargo`](#cargo) +* [`auto`](#auto) +* [`retry`](#retry) +* [`iterator`](#iterator) +* [`times`](#times), `timesSeries`, `timesLimit` + +### Utils + +* [`apply`](#apply) +* [`nextTick`](#nextTick) +* [`memoize`](#memoize) +* [`unmemoize`](#unmemoize) +* [`ensureAsync`](#ensureAsync) +* [`constant`](#constant) +* [`asyncify`](#asyncify) +* [`wrapSync`](#wrapSync) +* [`log`](#log) +* [`dir`](#dir) +* [`noConflict`](#noConflict) + +## Collections + + + +### each(arr, iterator, [callback]) + +Applies the function `iterator` to each item in `arr`, in parallel. +The `iterator` is called with an item from the list, and a callback for when it +has finished. If the `iterator` passes an error to its `callback`, the main +`callback` (for the `each` function) is immediately called with the error. + +Note, that since this function applies `iterator` to each item in parallel, +there is no guarantee that the iterator functions will complete in order. + +__Arguments__ + +* `arr` - An array to iterate over. +* `iterator(item, callback)` - A function to apply to each item in `arr`. + The iterator is passed a `callback(err)` which must be called once it has + completed. If no error has occurred, the `callback` should be run without + arguments or with an explicit `null` argument. The array index is not passed + to the iterator. If you need the index, use [`forEachOf`](#forEachOf). +* `callback(err)` - *Optional* A callback which is called when all `iterator` functions + have finished, or an error occurs. + +__Examples__ + + +```js +// assuming openFiles is an array of file names and saveFile is a function +// to save the modified contents of that file: + +async.each(openFiles, saveFile, function(err){ + // if any of the saves produced an error, err would equal that error +}); +``` + +```js +// assuming openFiles is an array of file names + +async.each(openFiles, function(file, callback) { + + // Perform operation on file here. + console.log('Processing file ' + file); + + if( file.length > 32 ) { + console.log('This file name is too long'); + callback('File name too long'); + } else { + // Do work to process file here + console.log('File processed'); + callback(); + } +}, function(err){ + // if any of the file processing produced an error, err would equal that error + if( err ) { + // One of the iterations produced an error. + // All processing will now stop. + console.log('A file failed to process'); + } else { + console.log('All files have been processed successfully'); + } +}); +``` + +__Related__ + +* eachSeries(arr, iterator, [callback]) +* eachLimit(arr, limit, iterator, [callback]) + +--------------------------------------- + + + + +### forEachOf(obj, iterator, [callback]) + +Like `each`, except that it iterates over objects, and passes the key as the second argument to the iterator. + +__Arguments__ + +* `obj` - An object or array to iterate over. +* `iterator(item, key, callback)` - A function to apply to each item in `obj`. +The `key` is the item's key, or index in the case of an array. The iterator is +passed a `callback(err)` which must be called once it has completed. If no +error has occurred, the callback should be run without arguments or with an +explicit `null` argument. +* `callback(err)` - *Optional* A callback which is called when all `iterator` functions have finished, or an error occurs. + +__Example__ + +```js +var obj = {dev: "/dev.json", test: "/test.json", prod: "/prod.json"}; +var configs = {}; + +async.forEachOf(obj, function (value, key, callback) { + fs.readFile(__dirname + value, "utf8", function (err, data) { + if (err) return callback(err); + try { + configs[key] = JSON.parse(data); + } catch (e) { + return callback(e); + } + callback(); + }) +}, function (err) { + if (err) console.error(err.message); + // configs is now a map of JSON data + doSomethingWith(configs); +}) +``` + +__Related__ + +* forEachOfSeries(obj, iterator, [callback]) +* forEachOfLimit(obj, limit, iterator, [callback]) + +--------------------------------------- + + +### map(arr, iterator, [callback]) + +Produces a new array of values by mapping each value in `arr` through +the `iterator` function. The `iterator` is called with an item from `arr` and a +callback for when it has finished processing. Each of these callback takes 2 arguments: +an `error`, and the transformed item from `arr`. If `iterator` passes an error to its +callback, the main `callback` (for the `map` function) is immediately called with the error. + +Note, that since this function applies the `iterator` to each item in parallel, +there is no guarantee that the `iterator` functions will complete in order. +However, the results array will be in the same order as the original `arr`. + +__Arguments__ + +* `arr` - An array to iterate over. +* `iterator(item, callback)` - A function to apply to each item in `arr`. + The iterator is passed a `callback(err, transformed)` which must be called once + it has completed with an error (which can be `null`) and a transformed item. +* `callback(err, results)` - *Optional* A callback which is called when all `iterator` + functions have finished, or an error occurs. Results is an array of the + transformed items from the `arr`. + +__Example__ + +```js +async.map(['file1','file2','file3'], fs.stat, function(err, results){ + // results is now an array of stats for each file +}); +``` + +__Related__ +* mapSeries(arr, iterator, [callback]) +* mapLimit(arr, limit, iterator, [callback]) + +--------------------------------------- + + + +### filter(arr, iterator, [callback]) + +__Alias:__ `select` + +Returns a new array of all the values in `arr` which pass an async truth test. +_The callback for each `iterator` call only accepts a single argument of `true` or +`false`; it does not accept an error argument first!_ This is in-line with the +way node libraries work with truth tests like `fs.exists`. This operation is +performed in parallel, but the results array will be in the same order as the +original. + +__Arguments__ + +* `arr` - An array to iterate over. +* `iterator(item, callback)` - A truth test to apply to each item in `arr`. + The `iterator` is passed a `callback(truthValue)`, which must be called with a + boolean argument once it has completed. +* `callback(results)` - *Optional* A callback which is called after all the `iterator` + functions have finished. + +__Example__ + +```js +async.filter(['file1','file2','file3'], fs.exists, function(results){ + // results now equals an array of the existing files +}); +``` + +__Related__ + +* filterSeries(arr, iterator, [callback]) +* filterLimit(arr, limit, iterator, [callback]) + +--------------------------------------- + + +### reject(arr, iterator, [callback]) + +The opposite of [`filter`](#filter). Removes values that pass an `async` truth test. + +__Related__ + +* rejectSeries(arr, iterator, [callback]) +* rejectLimit(arr, limit, iterator, [callback]) + +--------------------------------------- + + +### reduce(arr, memo, iterator, [callback]) + +__Aliases:__ `inject`, `foldl` + +Reduces `arr` into a single value using an async `iterator` to return +each successive step. `memo` is the initial state of the reduction. +This function only operates in series. + +For performance reasons, it may make sense to split a call to this function into +a parallel map, and then use the normal `Array.prototype.reduce` on the results. +This function is for situations where each step in the reduction needs to be async; +if you can get the data before reducing it, then it's probably a good idea to do so. + +__Arguments__ + +* `arr` - An array to iterate over. +* `memo` - The initial state of the reduction. +* `iterator(memo, item, callback)` - A function applied to each item in the + array to produce the next step in the reduction. The `iterator` is passed a + `callback(err, reduction)` which accepts an optional error as its first + argument, and the state of the reduction as the second. If an error is + passed to the callback, the reduction is stopped and the main `callback` is + immediately called with the error. +* `callback(err, result)` - *Optional* A callback which is called after all the `iterator` + functions have finished. Result is the reduced value. + +__Example__ + +```js +async.reduce([1,2,3], 0, function(memo, item, callback){ + // pointless async: + process.nextTick(function(){ + callback(null, memo + item) + }); +}, function(err, result){ + // result is now equal to the last value of memo, which is 6 +}); +``` + +--------------------------------------- + + +### reduceRight(arr, memo, iterator, [callback]) + +__Alias:__ `foldr` + +Same as [`reduce`](#reduce), only operates on `arr` in reverse order. + + +--------------------------------------- + + +### detect(arr, iterator, [callback]) + +Returns the first value in `arr` that passes an async truth test. The +`iterator` is applied in parallel, meaning the first iterator to return `true` will +fire the detect `callback` with that result. That means the result might not be +the first item in the original `arr` (in terms of order) that passes the test. + +If order within the original `arr` is important, then look at [`detectSeries`](#detectSeries). + +__Arguments__ + +* `arr` - An array to iterate over. +* `iterator(item, callback)` - A truth test to apply to each item in `arr`. + The iterator is passed a `callback(truthValue)` which must be called with a + boolean argument once it has completed. **Note: this callback does not take an error as its first argument.** +* `callback(result)` - *Optional* A callback which is called as soon as any iterator returns + `true`, or after all the `iterator` functions have finished. Result will be + the first item in the array that passes the truth test (iterator) or the + value `undefined` if none passed. **Note: this callback does not take an error as its first argument.** + +__Example__ + +```js +async.detect(['file1','file2','file3'], fs.exists, function(result){ + // result now equals the first file in the list that exists +}); +``` + +__Related__ + +* detectSeries(arr, iterator, [callback]) +* detectLimit(arr, limit, iterator, [callback]) + +--------------------------------------- + + +### sortBy(arr, iterator, [callback]) + +Sorts a list by the results of running each `arr` value through an async `iterator`. + +__Arguments__ + +* `arr` - An array to iterate over. +* `iterator(item, callback)` - A function to apply to each item in `arr`. + The iterator is passed a `callback(err, sortValue)` which must be called once it + has completed with an error (which can be `null`) and a value to use as the sort + criteria. +* `callback(err, results)` - *Optional* A callback which is called after all the `iterator` + functions have finished, or an error occurs. Results is the items from + the original `arr` sorted by the values returned by the `iterator` calls. + +__Example__ + +```js +async.sortBy(['file1','file2','file3'], function(file, callback){ + fs.stat(file, function(err, stats){ + callback(err, stats.mtime); + }); +}, function(err, results){ + // results is now the original array of files sorted by + // modified date +}); +``` + +__Sort Order__ + +By modifying the callback parameter the sorting order can be influenced: + +```js +//ascending order +async.sortBy([1,9,3,5], function(x, callback){ + callback(null, x); +}, function(err,result){ + //result callback +} ); + +//descending order +async.sortBy([1,9,3,5], function(x, callback){ + callback(null, x*-1); //<- x*-1 instead of x, turns the order around +}, function(err,result){ + //result callback +} ); +``` + +--------------------------------------- + + +### some(arr, iterator, [callback]) + +__Alias:__ `any` + +Returns `true` if at least one element in the `arr` satisfies an async test. +_The callback for each iterator call only accepts a single argument of `true` or +`false`; it does not accept an error argument first!_ This is in-line with the +way node libraries work with truth tests like `fs.exists`. Once any iterator +call returns `true`, the main `callback` is immediately called. + +__Arguments__ + +* `arr` - An array to iterate over. +* `iterator(item, callback)` - A truth test to apply to each item in the array + in parallel. The iterator is passed a `callback(truthValue)`` which must be + called with a boolean argument once it has completed. +* `callback(result)` - *Optional* A callback which is called as soon as any iterator returns + `true`, or after all the iterator functions have finished. Result will be + either `true` or `false` depending on the values of the async tests. + + **Note: the callbacks do not take an error as their first argument.** +__Example__ + +```js +async.some(['file1','file2','file3'], fs.exists, function(result){ + // if result is true then at least one of the files exists +}); +``` + +__Related__ + +* someLimit(arr, limit, iterator, callback) + +--------------------------------------- + + +### every(arr, iterator, [callback]) + +__Alias:__ `all` + +Returns `true` if every element in `arr` satisfies an async test. +_The callback for each `iterator` call only accepts a single argument of `true` or +`false`; it does not accept an error argument first!_ This is in-line with the +way node libraries work with truth tests like `fs.exists`. + +__Arguments__ + +* `arr` - An array to iterate over. +* `iterator(item, callback)` - A truth test to apply to each item in the array + in parallel. The iterator is passed a `callback(truthValue)` which must be + called with a boolean argument once it has completed. +* `callback(result)` - *Optional* A callback which is called as soon as any iterator returns + `false`, or after all the iterator functions have finished. Result will be + either `true` or `false` depending on the values of the async tests. + + **Note: the callbacks do not take an error as their first argument.** + +__Example__ + +```js +async.every(['file1','file2','file3'], fs.exists, function(result){ + // if result is true then every file exists +}); +``` + +__Related__ + +* everyLimit(arr, limit, iterator, callback) + +--------------------------------------- + + +### concat(arr, iterator, [callback]) + +Applies `iterator` to each item in `arr`, concatenating the results. Returns the +concatenated list. The `iterator`s are called in parallel, and the results are +concatenated as they return. There is no guarantee that the results array will +be returned in the original order of `arr` passed to the `iterator` function. + +__Arguments__ + +* `arr` - An array to iterate over. +* `iterator(item, callback)` - A function to apply to each item in `arr`. + The iterator is passed a `callback(err, results)` which must be called once it + has completed with an error (which can be `null`) and an array of results. +* `callback(err, results)` - *Optional* A callback which is called after all the `iterator` + functions have finished, or an error occurs. Results is an array containing + the concatenated results of the `iterator` function. + +__Example__ + +```js +async.concat(['dir1','dir2','dir3'], fs.readdir, function(err, files){ + // files is now a list of filenames that exist in the 3 directories +}); +``` + +__Related__ + +* concatSeries(arr, iterator, [callback]) + + +## Control Flow + + +### series(tasks, [callback]) + +Run the functions in the `tasks` array in series, each one running once the previous +function has completed. If any functions in the series pass an error to its +callback, no more functions are run, and `callback` is immediately called with the value of the error. +Otherwise, `callback` receives an array of results when `tasks` have completed. + +It is also possible to use an object instead of an array. Each property will be +run as a function, and the results will be passed to the final `callback` as an object +instead of an array. This can be a more readable way of handling results from +[`series`](#series). + +**Note** that while many implementations preserve the order of object properties, the +[ECMAScript Language Specification](http://www.ecma-international.org/ecma-262/5.1/#sec-8.6) +explicitly states that + +> The mechanics and order of enumerating the properties is not specified. + +So if you rely on the order in which your series of functions are executed, and want +this to work on all platforms, consider using an array. + +__Arguments__ + +* `tasks` - An array or object containing functions to run, each function is passed + a `callback(err, result)` it must call on completion with an error `err` (which can + be `null`) and an optional `result` value. +* `callback(err, results)` - An optional callback to run once all the functions + have completed. This function gets a results array (or object) containing all + the result arguments passed to the `task` callbacks. + +__Example__ + +```js +async.series([ + function(callback){ + // do some stuff ... + callback(null, 'one'); + }, + function(callback){ + // do some more stuff ... + callback(null, 'two'); + } +], +// optional callback +function(err, results){ + // results is now equal to ['one', 'two'] +}); + + +// an example using an object instead of an array +async.series({ + one: function(callback){ + setTimeout(function(){ + callback(null, 1); + }, 200); + }, + two: function(callback){ + setTimeout(function(){ + callback(null, 2); + }, 100); + } +}, +function(err, results) { + // results is now equal to: {one: 1, two: 2} +}); +``` + +--------------------------------------- + + +### parallel(tasks, [callback]) + +Run the `tasks` array of functions in parallel, without waiting until the previous +function has completed. If any of the functions pass an error to its +callback, the main `callback` is immediately called with the value of the error. +Once the `tasks` have completed, the results are passed to the final `callback` as an +array. + +**Note:** `parallel` is about kicking-off I/O tasks in parallel, not about parallel execution of code. If your tasks do not use any timers or perform any I/O, they will actually be executed in series. Any synchronous setup sections for each task will happen one after the other. JavaScript remains single-threaded. + +It is also possible to use an object instead of an array. Each property will be +run as a function and the results will be passed to the final `callback` as an object +instead of an array. This can be a more readable way of handling results from +[`parallel`](#parallel). + + +__Arguments__ + +* `tasks` - An array or object containing functions to run. Each function is passed + a `callback(err, result)` which it must call on completion with an error `err` + (which can be `null`) and an optional `result` value. +* `callback(err, results)` - An optional callback to run once all the functions + have completed successfully. This function gets a results array (or object) containing all + the result arguments passed to the task callbacks. + +__Example__ + +```js +async.parallel([ + function(callback){ + setTimeout(function(){ + callback(null, 'one'); + }, 200); + }, + function(callback){ + setTimeout(function(){ + callback(null, 'two'); + }, 100); + } +], +// optional callback +function(err, results){ + // the results array will equal ['one','two'] even though + // the second function had a shorter timeout. +}); + + +// an example using an object instead of an array +async.parallel({ + one: function(callback){ + setTimeout(function(){ + callback(null, 1); + }, 200); + }, + two: function(callback){ + setTimeout(function(){ + callback(null, 2); + }, 100); + } +}, +function(err, results) { + // results is now equals to: {one: 1, two: 2} +}); +``` + +__Related__ + +* parallelLimit(tasks, limit, [callback]) + +--------------------------------------- + + +### whilst(test, fn, callback) + +Repeatedly call `fn`, while `test` returns `true`. Calls `callback` when stopped, +or an error occurs. + +__Arguments__ + +* `test()` - synchronous truth test to perform before each execution of `fn`. +* `fn(callback)` - A function which is called each time `test` passes. The function is + passed a `callback(err)`, which must be called once it has completed with an + optional `err` argument. +* `callback(err, [results])` - A callback which is called after the test + function has failed and repeated execution of `fn` has stopped. `callback` + will be passed an error and any arguments passed to the final `fn`'s callback. + +__Example__ + +```js +var count = 0; + +async.whilst( + function () { return count < 5; }, + function (callback) { + count++; + setTimeout(function () { + callback(null, count); + }, 1000); + }, + function (err, n) { + // 5 seconds have passed, n = 5 + } +); +``` + +--------------------------------------- + + +### doWhilst(fn, test, callback) + +The post-check version of [`whilst`](#whilst). To reflect the difference in +the order of operations, the arguments `test` and `fn` are switched. + +`doWhilst` is to `whilst` as `do while` is to `while` in plain JavaScript. + +--------------------------------------- + + +### until(test, fn, callback) + +Repeatedly call `fn` until `test` returns `true`. Calls `callback` when stopped, +or an error occurs. `callback` will be passed an error and any arguments passed +to the final `fn`'s callback. + +The inverse of [`whilst`](#whilst). + +--------------------------------------- + + +### doUntil(fn, test, callback) + +Like [`doWhilst`](#doWhilst), except the `test` is inverted. Note the argument ordering differs from `until`. + +--------------------------------------- + + +### during(test, fn, callback) + +Like [`whilst`](#whilst), except the `test` is an asynchronous function that is passed a callback in the form of `function (err, truth)`. If error is passed to `test` or `fn`, the main callback is immediately called with the value of the error. + +__Example__ + +```js +var count = 0; + +async.during( + function (callback) { + return callback(null, count < 5); + }, + function (callback) { + count++; + setTimeout(callback, 1000); + }, + function (err) { + // 5 seconds have passed + } +); +``` + +--------------------------------------- + + +### doDuring(fn, test, callback) + +The post-check version of [`during`](#during). To reflect the difference in +the order of operations, the arguments `test` and `fn` are switched. + +Also a version of [`doWhilst`](#doWhilst) with asynchronous `test` function. + +--------------------------------------- + + +### forever(fn, [errback]) + +Calls the asynchronous function `fn` with a callback parameter that allows it to +call itself again, in series, indefinitely. + +If an error is passed to the callback then `errback` is called with the +error, and execution stops, otherwise it will never be called. + +```js +async.forever( + function(next) { + // next is suitable for passing to things that need a callback(err [, whatever]); + // it will result in this function being called again. + }, + function(err) { + // if next is called with a value in its first parameter, it will appear + // in here as 'err', and execution will stop. + } +); +``` + +--------------------------------------- + + +### waterfall(tasks, [callback]) + +Runs the `tasks` array of functions in series, each passing their results to the next in +the array. However, if any of the `tasks` pass an error to their own callback, the +next function is not executed, and the main `callback` is immediately called with +the error. + +__Arguments__ + +* `tasks` - An array of functions to run, each function is passed a + `callback(err, result1, result2, ...)` it must call on completion. The first + argument is an error (which can be `null`) and any further arguments will be + passed as arguments in order to the next task. +* `callback(err, [results])` - An optional callback to run once all the functions + have completed. This will be passed the results of the last task's callback. + + + +__Example__ + +```js +async.waterfall([ + function(callback) { + callback(null, 'one', 'two'); + }, + function(arg1, arg2, callback) { + // arg1 now equals 'one' and arg2 now equals 'two' + callback(null, 'three'); + }, + function(arg1, callback) { + // arg1 now equals 'three' + callback(null, 'done'); + } +], function (err, result) { + // result now equals 'done' +}); +``` + +--------------------------------------- + +### compose(fn1, fn2...) + +Creates a function which is a composition of the passed asynchronous +functions. Each function consumes the return value of the function that +follows. Composing functions `f()`, `g()`, and `h()` would produce the result of +`f(g(h()))`, only this version uses callbacks to obtain the return values. + +Each function is executed with the `this` binding of the composed function. + +__Arguments__ + +* `functions...` - the asynchronous functions to compose + + +__Example__ + +```js +function add1(n, callback) { + setTimeout(function () { + callback(null, n + 1); + }, 10); +} + +function mul3(n, callback) { + setTimeout(function () { + callback(null, n * 3); + }, 10); +} + +var add1mul3 = async.compose(mul3, add1); + +add1mul3(4, function (err, result) { + // result now equals 15 +}); +``` + +--------------------------------------- + +### seq(fn1, fn2...) + +Version of the compose function that is more natural to read. +Each function consumes the return value of the previous function. +It is the equivalent of [`compose`](#compose) with the arguments reversed. + +Each function is executed with the `this` binding of the composed function. + +__Arguments__ + +* `functions...` - the asynchronous functions to compose + + +__Example__ + +```js +// Requires lodash (or underscore), express3 and dresende's orm2. +// Part of an app, that fetches cats of the logged user. +// This example uses `seq` function to avoid overnesting and error +// handling clutter. +app.get('/cats', function(request, response) { + var User = request.models.User; + async.seq( + _.bind(User.get, User), // 'User.get' has signature (id, callback(err, data)) + function(user, fn) { + user.getCats(fn); // 'getCats' has signature (callback(err, data)) + } + )(req.session.user_id, function (err, cats) { + if (err) { + console.error(err); + response.json({ status: 'error', message: err.message }); + } else { + response.json({ status: 'ok', message: 'Cats found', data: cats }); + } + }); +}); +``` + +--------------------------------------- + +### applyEach(fns, args..., callback) + +Applies the provided arguments to each function in the array, calling +`callback` after all functions have completed. If you only provide the first +argument, then it will return a function which lets you pass in the +arguments as if it were a single function call. + +__Arguments__ + +* `fns` - the asynchronous functions to all call with the same arguments +* `args...` - any number of separate arguments to pass to the function +* `callback` - the final argument should be the callback, called when all + functions have completed processing + + +__Example__ + +```js +async.applyEach([enableSearch, updateSchema], 'bucket', callback); + +// partial application example: +async.each( + buckets, + async.applyEach([enableSearch, updateSchema]), + callback +); +``` + +__Related__ + +* applyEachSeries(tasks, args..., [callback]) + +--------------------------------------- + + +### queue(worker, [concurrency]) + +Creates a `queue` object with the specified `concurrency`. Tasks added to the +`queue` are processed in parallel (up to the `concurrency` limit). If all +`worker`s are in progress, the task is queued until one becomes available. +Once a `worker` completes a `task`, that `task`'s callback is called. + +__Arguments__ + +* `worker(task, callback)` - An asynchronous function for processing a queued + task, which must call its `callback(err)` argument when finished, with an + optional `error` as an argument. If you want to handle errors from an individual task, pass a callback to `q.push()`. +* `concurrency` - An `integer` for determining how many `worker` functions should be + run in parallel. If omitted, the concurrency defaults to `1`. If the concurrency is `0`, an error is thrown. + +__Queue objects__ + +The `queue` object returned by this function has the following properties and +methods: + +* `length()` - a function returning the number of items waiting to be processed. +* `started` - a function returning whether or not any items have been pushed and processed by the queue +* `running()` - a function returning the number of items currently being processed. +* `workersList()` - a function returning the array of items currently being processed. +* `idle()` - a function returning false if there are items waiting or being processed, or true if not. +* `concurrency` - an integer for determining how many `worker` functions should be + run in parallel. This property can be changed after a `queue` is created to + alter the concurrency on-the-fly. +* `push(task, [callback])` - add a new task to the `queue`. Calls `callback` once + the `worker` has finished processing the task. Instead of a single task, a `tasks` array + can be submitted. The respective callback is used for every task in the list. +* `unshift(task, [callback])` - add a new task to the front of the `queue`. +* `saturated` - a callback that is called when the `queue` length hits the `concurrency` limit, + and further tasks will be queued. +* `empty` - a callback that is called when the last item from the `queue` is given to a `worker`. +* `drain` - a callback that is called when the last item from the `queue` has returned from the `worker`. +* `paused` - a boolean for determining whether the queue is in a paused state +* `pause()` - a function that pauses the processing of tasks until `resume()` is called. +* `resume()` - a function that resumes the processing of queued tasks when the queue is paused. +* `kill()` - a function that removes the `drain` callback and empties remaining tasks from the queue forcing it to go idle. + +__Example__ + +```js +// create a queue object with concurrency 2 + +var q = async.queue(function (task, callback) { + console.log('hello ' + task.name); + callback(); +}, 2); + + +// assign a callback +q.drain = function() { + console.log('all items have been processed'); +} + +// add some items to the queue + +q.push({name: 'foo'}, function (err) { + console.log('finished processing foo'); +}); +q.push({name: 'bar'}, function (err) { + console.log('finished processing bar'); +}); + +// add some items to the queue (batch-wise) + +q.push([{name: 'baz'},{name: 'bay'},{name: 'bax'}], function (err) { + console.log('finished processing item'); +}); + +// add some items to the front of the queue + +q.unshift({name: 'bar'}, function (err) { + console.log('finished processing bar'); +}); +``` + + +--------------------------------------- + + +### priorityQueue(worker, concurrency) + +The same as [`queue`](#queue) only tasks are assigned a priority and completed in ascending priority order. There are two differences between `queue` and `priorityQueue` objects: + +* `push(task, priority, [callback])` - `priority` should be a number. If an array of + `tasks` is given, all tasks will be assigned the same priority. +* The `unshift` method was removed. + +--------------------------------------- + + +### cargo(worker, [payload]) + +Creates a `cargo` object with the specified payload. Tasks added to the +cargo will be processed altogether (up to the `payload` limit). If the +`worker` is in progress, the task is queued until it becomes available. Once +the `worker` has completed some tasks, each callback of those tasks is called. +Check out [these](https://camo.githubusercontent.com/6bbd36f4cf5b35a0f11a96dcd2e97711ffc2fb37/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f313637363837312f36383130382f62626330636662302d356632392d313165322d393734662d3333393763363464633835382e676966) [animations](https://camo.githubusercontent.com/f4810e00e1c5f5f8addbe3e9f49064fd5d102699/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f313637363837312f36383130312f38346339323036362d356632392d313165322d383134662d3964336430323431336266642e676966) for how `cargo` and `queue` work. + +While [queue](#queue) passes only one task to one of a group of workers +at a time, cargo passes an array of tasks to a single worker, repeating +when the worker is finished. + +__Arguments__ + +* `worker(tasks, callback)` - An asynchronous function for processing an array of + queued tasks, which must call its `callback(err)` argument when finished, with + an optional `err` argument. +* `payload` - An optional `integer` for determining how many tasks should be + processed per round; if omitted, the default is unlimited. + +__Cargo objects__ + +The `cargo` object returned by this function has the following properties and +methods: + +* `length()` - A function returning the number of items waiting to be processed. +* `payload` - An `integer` for determining how many tasks should be + process per round. This property can be changed after a `cargo` is created to + alter the payload on-the-fly. +* `push(task, [callback])` - Adds `task` to the `queue`. The callback is called + once the `worker` has finished processing the task. Instead of a single task, an array of `tasks` + can be submitted. The respective callback is used for every task in the list. +* `saturated` - A callback that is called when the `queue.length()` hits the concurrency and further tasks will be queued. +* `empty` - A callback that is called when the last item from the `queue` is given to a `worker`. +* `drain` - A callback that is called when the last item from the `queue` has returned from the `worker`. +* `idle()`, `pause()`, `resume()`, `kill()` - cargo inherits all of the same methods and event calbacks as [`queue`](#queue) + +__Example__ + +```js +// create a cargo object with payload 2 + +var cargo = async.cargo(function (tasks, callback) { + for(var i=0; i +### auto(tasks, [concurrency], [callback]) + +Determines the best order for running the functions in `tasks`, based on their requirements. Each function can optionally depend on other functions being completed first, and each function is run as soon as its requirements are satisfied. + +If any of the functions pass an error to their callback, the `auto` sequence will stop. Further tasks will not execute (so any other functions depending on it will not run), and the main `callback` is immediately called with the error. Functions also receive an object containing the results of functions which have completed so far. + +Note, all functions are called with a `results` object as a second argument, +so it is unsafe to pass functions in the `tasks` object which cannot handle the +extra argument. + +For example, this snippet of code: + +```js +async.auto({ + readData: async.apply(fs.readFile, 'data.txt', 'utf-8') +}, callback); +``` + +will have the effect of calling `readFile` with the results object as the last +argument, which will fail: + +```js +fs.readFile('data.txt', 'utf-8', cb, {}); +``` + +Instead, wrap the call to `readFile` in a function which does not forward the +`results` object: + +```js +async.auto({ + readData: function(cb, results){ + fs.readFile('data.txt', 'utf-8', cb); + } +}, callback); +``` + +__Arguments__ + +* `tasks` - An object. Each of its properties is either a function or an array of + requirements, with the function itself the last item in the array. The object's key + of a property serves as the name of the task defined by that property, + i.e. can be used when specifying requirements for other tasks. + The function receives two arguments: (1) a `callback(err, result)` which must be + called when finished, passing an `error` (which can be `null`) and the result of + the function's execution, and (2) a `results` object, containing the results of + the previously executed functions. +* `concurrency` - An optional `integer` for determining the maximum number of tasks that can be run in parallel. By default, as many as possible. +* `callback(err, results)` - An optional callback which is called when all the + tasks have been completed. It receives the `err` argument if any `tasks` + pass an error to their callback. Results are always returned; however, if + an error occurs, no further `tasks` will be performed, and the results + object will only contain partial results. + + +__Example__ + +```js +async.auto({ + get_data: function(callback){ + console.log('in get_data'); + // async code to get some data + callback(null, 'data', 'converted to array'); + }, + make_folder: function(callback){ + console.log('in make_folder'); + // async code to create a directory to store a file in + // this is run at the same time as getting the data + callback(null, 'folder'); + }, + write_file: ['get_data', 'make_folder', function(callback, results){ + console.log('in write_file', JSON.stringify(results)); + // once there is some data and the directory exists, + // write the data to a file in the directory + callback(null, 'filename'); + }], + email_link: ['write_file', function(callback, results){ + console.log('in email_link', JSON.stringify(results)); + // once the file is written let's email a link to it... + // results.write_file contains the filename returned by write_file. + callback(null, {'file':results.write_file, 'email':'user@example.com'}); + }] +}, function(err, results) { + console.log('err = ', err); + console.log('results = ', results); +}); +``` + +This is a fairly trivial example, but to do this using the basic parallel and +series functions would look like this: + +```js +async.parallel([ + function(callback){ + console.log('in get_data'); + // async code to get some data + callback(null, 'data', 'converted to array'); + }, + function(callback){ + console.log('in make_folder'); + // async code to create a directory to store a file in + // this is run at the same time as getting the data + callback(null, 'folder'); + } +], +function(err, results){ + async.series([ + function(callback){ + console.log('in write_file', JSON.stringify(results)); + // once there is some data and the directory exists, + // write the data to a file in the directory + results.push('filename'); + callback(null); + }, + function(callback){ + console.log('in email_link', JSON.stringify(results)); + // once the file is written let's email a link to it... + callback(null, {'file':results.pop(), 'email':'user@example.com'}); + } + ]); +}); +``` + +For a complicated series of `async` tasks, using the [`auto`](#auto) function makes adding +new tasks much easier (and the code more readable). + + +--------------------------------------- + + +### retry([opts = {times: 5, interval: 0}| 5], task, [callback]) + +Attempts to get a successful response from `task` no more than `times` times before +returning an error. If the task is successful, the `callback` will be passed the result +of the successful task. If all attempts fail, the callback will be passed the error and +result (if any) of the final attempt. + +__Arguments__ + +* `opts` - Can be either an object with `times` and `interval` or a number. `times` is how many attempts should be made before giving up. `interval` is how long to wait inbetween attempts. Defaults to {times: 5, interval: 0} + * if a number is passed in it sets `times` only (with `interval` defaulting to 0). +* `task(callback, results)` - A function which receives two arguments: (1) a `callback(err, result)` + which must be called when finished, passing `err` (which can be `null`) and the `result` of + the function's execution, and (2) a `results` object, containing the results of + the previously executed functions (if nested inside another control flow). +* `callback(err, results)` - An optional callback which is called when the + task has succeeded, or after the final failed attempt. It receives the `err` and `result` arguments of the last attempt at completing the `task`. + +The [`retry`](#retry) function can be used as a stand-alone control flow by passing a +callback, as shown below: + +```js +async.retry(3, apiMethod, function(err, result) { + // do something with the result +}); +``` + +```js +async.retry({times: 3, interval: 200}, apiMethod, function(err, result) { + // do something with the result +}); +``` + +It can also be embedded within other control flow functions to retry individual methods +that are not as reliable, like this: + +```js +async.auto({ + users: api.getUsers.bind(api), + payments: async.retry(3, api.getPayments.bind(api)) +}, function(err, results) { + // do something with the results +}); +``` + + +--------------------------------------- + + +### iterator(tasks) + +Creates an iterator function which calls the next function in the `tasks` array, +returning a continuation to call the next one after that. It's also possible to +“peek” at the next iterator with `iterator.next()`. + +This function is used internally by the `async` module, but can be useful when +you want to manually control the flow of functions in series. + +__Arguments__ + +* `tasks` - An array of functions to run. + +__Example__ + +```js +var iterator = async.iterator([ + function(){ sys.p('one'); }, + function(){ sys.p('two'); }, + function(){ sys.p('three'); } +]); + +node> var iterator2 = iterator(); +'one' +node> var iterator3 = iterator2(); +'two' +node> iterator3(); +'three' +node> var nextfn = iterator2.next(); +node> nextfn(); +'three' +``` + +--------------------------------------- + + +### apply(function, arguments..) + +Creates a continuation function with some arguments already applied. + +Useful as a shorthand when combined with other control flow functions. Any arguments +passed to the returned function are added to the arguments originally passed +to apply. + +__Arguments__ + +* `function` - The function you want to eventually apply all arguments to. +* `arguments...` - Any number of arguments to automatically apply when the + continuation is called. + +__Example__ + +```js +// using apply + +async.parallel([ + async.apply(fs.writeFile, 'testfile1', 'test1'), + async.apply(fs.writeFile, 'testfile2', 'test2'), +]); + + +// the same process without using apply + +async.parallel([ + function(callback){ + fs.writeFile('testfile1', 'test1', callback); + }, + function(callback){ + fs.writeFile('testfile2', 'test2', callback); + } +]); +``` + +It's possible to pass any number of additional arguments when calling the +continuation: + +```js +node> var fn = async.apply(sys.puts, 'one'); +node> fn('two', 'three'); +one +two +three +``` + +--------------------------------------- + + +### nextTick(callback), setImmediate(callback) + +Calls `callback` on a later loop around the event loop. In Node.js this just +calls `process.nextTick`; in the browser it falls back to `setImmediate(callback)` +if available, otherwise `setTimeout(callback, 0)`, which means other higher priority +events may precede the execution of `callback`. + +This is used internally for browser-compatibility purposes. + +__Arguments__ + +* `callback` - The function to call on a later loop around the event loop. + +__Example__ + +```js +var call_order = []; +async.nextTick(function(){ + call_order.push('two'); + // call_order now equals ['one','two'] +}); +call_order.push('one') +``` + + +### times(n, iterator, [callback]) + +Calls the `iterator` function `n` times, and accumulates results in the same manner +you would use with [`map`](#map). + +__Arguments__ + +* `n` - The number of times to run the function. +* `iterator` - The function to call `n` times. +* `callback` - see [`map`](#map) + +__Example__ + +```js +// Pretend this is some complicated async factory +var createUser = function(id, callback) { + callback(null, { + id: 'user' + id + }) +} +// generate 5 users +async.times(5, function(n, next){ + createUser(n, function(err, user) { + next(err, user) + }) +}, function(err, users) { + // we should now have 5 users +}); +``` + +__Related__ + +* timesSeries(n, iterator, [callback]) +* timesLimit(n, limit, iterator, [callback]) + + +## Utils + + +### memoize(fn, [hasher]) + +Caches the results of an `async` function. When creating a hash to store function +results against, the callback is omitted from the hash and an optional hash +function can be used. + +If no hash function is specified, the first argument is used as a hash key, which may work reasonably if it is a string or a data type that converts to a distinct string. Note that objects and arrays will not behave reasonably. Neither will cases where the other arguments are significant. In such cases, specify your own hash function. + +The cache of results is exposed as the `memo` property of the function returned +by `memoize`. + +__Arguments__ + +* `fn` - The function to proxy and cache results from. +* `hasher` - An optional function for generating a custom hash for storing + results. It has all the arguments applied to it apart from the callback, and + must be synchronous. + +__Example__ + +```js +var slow_fn = function (name, callback) { + // do something + callback(null, result); +}; +var fn = async.memoize(slow_fn); + +// fn can now be used as if it were slow_fn +fn('some name', function () { + // callback +}); +``` + + +### unmemoize(fn) + +Undoes a [`memoize`](#memoize)d function, reverting it to the original, unmemoized +form. Handy for testing. + +__Arguments__ + +* `fn` - the memoized function + +--------------------------------------- + + +### ensureAsync(fn) + +Wrap an async function and ensure it calls its callback on a later tick of the event loop. If the function already calls its callback on a next tick, no extra deferral is added. This is useful for preventing stack overflows (`RangeError: Maximum call stack size exceeded`) and generally keeping [Zalgo](http://blog.izs.me/post/59142742143/designing-apis-for-asynchrony) contained. + +__Arguments__ + +* `fn` - an async function, one that expects a node-style callback as its last argument + +Returns a wrapped function with the exact same call signature as the function passed in. + +__Example__ + +```js +function sometimesAsync(arg, callback) { + if (cache[arg]) { + return callback(null, cache[arg]); // this would be synchronous!! + } else { + doSomeIO(arg, callback); // this IO would be asynchronous + } +} + +// this has a risk of stack overflows if many results are cached in a row +async.mapSeries(args, sometimesAsync, done); + +// this will defer sometimesAsync's callback if necessary, +// preventing stack overflows +async.mapSeries(args, async.ensureAsync(sometimesAsync), done); + +``` + +--------------------------------------- + + +### constant(values...) + +Returns a function that when called, calls-back with the values provided. Useful as the first function in a `waterfall`, or for plugging values in to `auto`. + +__Example__ + +```js +async.waterfall([ + async.constant(42), + function (value, next) { + // value === 42 + }, + //... +], callback); + +async.waterfall([ + async.constant(filename, "utf8"), + fs.readFile, + function (fileData, next) { + //... + } + //... +], callback); + +async.auto({ + hostname: async.constant("https://server.net/"), + port: findFreePort, + launchServer: ["hostname", "port", function (cb, options) { + startServer(options, cb); + }], + //... +}, callback); + +``` + +--------------------------------------- + + + +### asyncify(func) + +__Alias:__ `wrapSync` + +Take a sync function and make it async, passing its return value to a callback. This is useful for plugging sync functions into a waterfall, series, or other async functions. Any arguments passed to the generated function will be passed to the wrapped function (except for the final callback argument). Errors thrown will be passed to the callback. + +__Example__ + +```js +async.waterfall([ + async.apply(fs.readFile, filename, "utf8"), + async.asyncify(JSON.parse), + function (data, next) { + // data is the result of parsing the text. + // If there was a parsing error, it would have been caught. + } +], callback) +``` + +--------------------------------------- + + +### log(function, arguments) + +Logs the result of an `async` function to the `console`. Only works in Node.js or +in browsers that support `console.log` and `console.error` (such as FF and Chrome). +If multiple arguments are returned from the async function, `console.log` is +called on each argument in order. + +__Arguments__ + +* `function` - The function you want to eventually apply all arguments to. +* `arguments...` - Any number of arguments to apply to the function. + +__Example__ + +```js +var hello = function(name, callback){ + setTimeout(function(){ + callback(null, 'hello ' + name); + }, 1000); +}; +``` +```js +node> async.log(hello, 'world'); +'hello world' +``` + +--------------------------------------- + + +### dir(function, arguments) + +Logs the result of an `async` function to the `console` using `console.dir` to +display the properties of the resulting object. Only works in Node.js or +in browsers that support `console.dir` and `console.error` (such as FF and Chrome). +If multiple arguments are returned from the async function, `console.dir` is +called on each argument in order. + +__Arguments__ + +* `function` - The function you want to eventually apply all arguments to. +* `arguments...` - Any number of arguments to apply to the function. + +__Example__ + +```js +var hello = function(name, callback){ + setTimeout(function(){ + callback(null, {hello: name}); + }, 1000); +}; +``` +```js +node> async.dir(hello, 'world'); +{hello: 'world'} +``` + +--------------------------------------- + + +### noConflict() + +Changes the value of `async` back to its original value, returning a reference to the +`async` object. diff --git a/build/async.js b/build/async.js index 84513d75e..e5d60e308 100644 --- a/build/async.js +++ b/build/async.js @@ -1,9 +1,30 @@ (function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : - factory((global.async = {})); + (factory((global.async = {}))); }(this, function (exports) { 'use strict'; + /** + * A faster alternative to `Function#apply`, this function invokes `func` + * with the `this` binding of `thisArg` and the arguments of `args`. + * + * @private + * @param {Function} func The function to invoke. + * @param {*} thisArg The `this` binding of `func`. + * @param {...*} [args] The arguments to invoke `func` with. + * @returns {*} Returns the result of `func`. + */ + function apply$1(func, thisArg, args) { + var length = args ? args.length : 0; + switch (length) { + case 0: return func.call(thisArg); + case 1: return func.call(thisArg, args[0]); + case 2: return func.call(thisArg, args[0], args[1]); + case 3: return func.call(thisArg, args[0], args[1], args[2]); + } + return func.apply(thisArg, args); + } + /** * Checks if `value` is the [language type](https://es5.github.io/#x8) of `Object`. * (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`) @@ -34,27 +55,6 @@ return !!value && (type == 'object' || type == 'function'); } - /** - * A faster alternative to `Function#apply`, this function invokes `func` - * with the `this` binding of `thisArg` and the arguments of `args`. - * - * @private - * @param {Function} func The function to invoke. - * @param {*} thisArg The `this` binding of `func`. - * @param {...*} [args] The arguments to invoke `func` with. - * @returns {*} Returns the result of `func`. - */ - function apply$1(func, thisArg, args) { - var length = args ? args.length : 0; - switch (length) { - case 0: return func.call(thisArg); - case 1: return func.call(thisArg, args[0]); - case 2: return func.call(thisArg, args[0], args[1]); - case 3: return func.call(thisArg, args[0], args[1], args[2]); - } - return func.apply(thisArg, args); - } - var funcTag = '[object Function]'; var genTag = '[object GeneratorFunction]'; /** Used for built-in method references. */ @@ -241,77 +241,23 @@ }; } - function asyncify(func) { - return rest(function (args) { - var callback = args.pop(); - var result; - try { - result = func.apply(this, args); - } catch (e) { - return callback(e); - } - // if result is Promise object - if (isObject(result) && typeof result.then === 'function') { - result.then(function (value) { - callback(null, value); - })['catch'](function (err) { - callback(err.message ? err : new Error(err)); - }); + function applyEach$1(eachfn) { + return rest(function (fns, args) { + var go = rest(function (args) { + var that = this; + var callback = args.pop(); + return eachfn(fns, function (fn, _, cb) { + fn.apply(that, args.concat([cb])); + }, callback); + }); + if (args.length) { + return go.apply(this, args); } else { - callback(null, result); + return go; } }); } - /** - * A specialized version of `_.map` for arrays without support for iteratee - * shorthands. - * - * @private - * @param {Array} array The array to iterate over. - * @param {Function} iteratee The function invoked per iteration. - * @returns {Array} Returns the new mapped array. - */ - function arrayMap(array, iteratee) { - var index = -1, - length = array.length, - result = Array(length); - - while (++index < length) { - result[index] = iteratee(array[index], index, array); - } - return result; - } - - /** - * The base implementation of `_.property` without support for deep paths. - * - * @private - * @param {string} key The key of the property to get. - * @returns {Function} Returns the new function. - */ - function baseProperty(key) { - return function(object) { - return object == null ? undefined : object[key]; - }; - } - - function _filter(eachfn, arr, iterator, callback) { - var results = []; - eachfn(arr, function (x, index, callback) { - iterator(x, function (v) { - if (v) { - results.push({ index: index, value: x }); - } - callback(); - }); - }, function () { - callback(arrayMap(results.sort(function (a, b) { - return a.index - b.index; - }), baseProperty('value'))); - }); - } - /** Used as the `TypeError` message for "Functions" methods. */ var FUNC_ERROR_TEXT$1 = 'Expected a function'; @@ -387,6 +333,19 @@ // No operation performed. } + /** + * The base implementation of `_.property` without support for deep paths. + * + * @private + * @param {string} key The key of the property to get. + * @returns {Function} Returns the new function. + */ + function baseProperty(key) { + return function(object) { + return object == null ? undefined : object[key]; + }; + } + /** * Gets the "length" property value of `object`. * @@ -799,6 +758,36 @@ }; } + function eachOf(object, iterator, callback) { + callback = once(callback || noop); + object = object || []; + + var iter = keyIterator(object); + var key, + completed = 0; + + while ((key = iter()) != null) { + completed += 1; + iterator(object[key], key, onlyOnce(done)); + } + + if (completed === 0) callback(null); + + function done(err) { + completed--; + if (err) { + callback(err); + } + // Check key is null in case iterator isn't exhausted + // and done resolved synchronously. + else if (key === null && completed <= 0) { + callback(null); + } + } + } + + var applyEach = applyEach$1(eachOf); + var _setImmediate = typeof setImmediate === 'function' && setImmediate; var _delay; @@ -849,137 +838,126 @@ iterate(); } - function doSeries(fn) { - return function (obj, iterator, callback) { - return fn(eachOfSeries, obj, iterator, callback); - }; - } + var applyEachSeries = applyEach$1(eachOfSeries); - var filterSeries = doSeries(_filter); + var apply = rest(function (fn, args) { + return rest(function (callArgs) { + return fn.apply(null, args.concat(callArgs)); + }); + }); - function _eachOfLimit(limit) { - return function (obj, iterator, callback) { - callback = once(callback || noop); - obj = obj || []; - var nextKey = keyIterator(obj); - if (limit <= 0) { - return callback(null); + function asyncify(func) { + return rest(function (args) { + var callback = args.pop(); + var result; + try { + result = func.apply(this, args); + } catch (e) { + return callback(e); } - var done = false; - var running = 0; - var errored = false; - - (function replenish() { - if (done && running <= 0) { - return callback(null); - } - - while (running < limit && !errored) { - var key = nextKey(); - if (key === null) { - done = true; - if (running <= 0) { - callback(null); - } - return; - } - running += 1; - iterator(obj[key], key, onlyOnce(function (err) { - running -= 1; - if (err) { - callback(err); - errored = true; - } else { - replenish(); - } - })); - } - })(); - }; + // if result is Promise object + if (isObject(result) && typeof result.then === 'function') { + result.then(function (value) { + callback(null, value); + })['catch'](function (err) { + callback(err.message ? err : new Error(err)); + }); + } else { + callback(null, result); + } + }); } - function doParallelLimit(fn) { - return function (obj, limit, iterator, callback) { - return fn(_eachOfLimit(limit), obj, iterator, callback); - }; + /** + * A specialized version of `_.forEach` for arrays without support for + * iteratee shorthands. + * + * @private + * @param {Array} array The array to iterate over. + * @param {Function} iteratee The function invoked per iteration. + * @returns {Array} Returns `array`. + */ + function arrayEach(array, iteratee) { + var index = -1, + length = array.length; + + while (++index < length) { + if (iteratee(array[index], index, array) === false) { + break; + } + } + return array; } - var filterLimit = doParallelLimit(_filter); - - function eachOf(object, iterator, callback) { - callback = once(callback || noop); - object = object || []; - - var iter = keyIterator(object); - var key, - completed = 0; - - while ((key = iter()) != null) { - completed += 1; - iterator(object[key], key, onlyOnce(done)); - } - - if (completed === 0) callback(null); + /** + * A specialized version of `_.every` for arrays without support for + * iteratee shorthands. + * + * @private + * @param {Array} array The array to iterate over. + * @param {Function} predicate The function invoked per iteration. + * @returns {boolean} Returns `true` if all elements pass the predicate check, else `false`. + */ + function arrayEvery(array, predicate) { + var index = -1, + length = array.length; - function done(err) { - completed--; - if (err) { - callback(err); - } - // Check key is null in case iterator isn't exhausted - // and done resolved synchronously. - else if (key === null && completed <= 0) { - callback(null); - } + while (++index < length) { + if (!predicate(array[index], index, array)) { + return false; } + } + return true; } - function doParallel(fn) { - return function (obj, iterator, callback) { - return fn(eachOf, obj, iterator, callback); - }; - } - - var filter = doParallel(_filter); - - function reduce(arr, memo, iterator, cb) { - eachOfSeries(arr, function (x, i, cb) { - iterator(memo, x, function (err, v) { - memo = v; - cb(err); - }); - }, function (err) { - cb(err, memo); - }); - } - - var slice = Array.prototype.slice; - - function reduceRight(arr, memo, iterator, cb) { - var reversed = slice.call(arr).reverse(); - reduce(reversed, memo, iterator, cb); - } - - function eachOfLimit(obj, limit, iterator, cb) { - _eachOfLimit(limit)(obj, iterator, cb); - } - - function _withoutIndex(iterator) { - return function (value, index, callback) { - return iterator(value, callback); - }; - } + /** + * Creates a base function for methods like `_.forIn`. + * + * @private + * @param {boolean} [fromRight] Specify iterating from right to left. + * @returns {Function} Returns the new base function. + */ + function createBaseFor(fromRight) { + return function(object, iteratee, keysFunc) { + var index = -1, + iterable = Object(object), + props = keysFunc(object), + length = props.length; - function eachLimit(arr, limit, iterator, cb) { - return _eachOfLimit(limit)(arr, _withoutIndex(iterator), cb); + while (length--) { + var key = props[fromRight ? length : ++index]; + if (iteratee(iterable[key], key, iterable) === false) { + break; + } + } + return object; + }; } - function eachSeries(arr, iterator, cb) { - return eachOfSeries(arr, _withoutIndex(iterator), cb); - } + /** + * The base implementation of `baseForIn` and `baseForOwn` which iterates + * over `object` properties returned by `keysFunc` invoking `iteratee` for + * each property. Iteratee functions may exit iteration early by explicitly + * returning `false`. + * + * @private + * @param {Object} object The object to iterate over. + * @param {Function} iteratee The function invoked per iteration. + * @param {Function} keysFunc The function to get the keys of `object`. + * @returns {Object} Returns `object`. + */ + var baseFor = createBaseFor(); - function each(arr, iterator, cb) { - return eachOf(arr, _withoutIndex(iterator), cb); + /** + * The base implementation of `_.forOwn` without support for iteratee shorthands. + * + * @private + * @param {Object} object The object to iterate over. + * @param {Function} iteratee The function invoked per iteration. + * @returns {Object} Returns `object`. + */ + function baseForOwn(object, iteratee) { + return object && baseFor(object, iteratee, keys); } /** @@ -1001,395 +979,274 @@ return value; } - function _createTester(eachfn, check, getResult) { - return function (arr, limit, iterator, cb) { - function done() { - if (cb) cb(getResult(false, void 0)); - } - function iteratee(x, _, callback) { - if (!cb) return callback(); - iterator(x, function (v) { - if (cb && check(v)) { - cb(getResult(true, x)); - cb = iterator = false; - } - callback(); - }); - } - if (arguments.length > 3) { - eachfn(arr, limit, iteratee, done); - } else { - cb = iterator; - iterator = limit; - eachfn(arr, iteratee, done); - } - }; - } - - var some = _createTester(eachOf, Boolean, identity); - - function notId(v) { - return !v; + /** + * Converts `value` to a function if it's not one. + * + * @private + * @param {*} value The value to process. + * @returns {Function} Returns the function. + */ + function toFunction(value) { + return typeof value == 'function' ? value : identity; } - var every = _createTester(eachOf, notId, notId); - - function whilst(test, iterator, cb) { - cb = cb || noop; - if (!test()) return cb(null); - var next = rest(function (err, args) { - if (err) return cb(err); - if (test.apply(this, args)) return iterator(next); - cb.apply(null, [null].concat(args)); - }); - iterator(next); + /** + * Iterates over own enumerable properties of an object invoking `iteratee` + * for each property. The iteratee is invoked with three arguments: + * (value, key, object). Iteratee functions may exit iteration early by + * explicitly returning `false`. + * + * @static + * @memberOf _ + * @category Object + * @param {Object} object The object to iterate over. + * @param {Function} [iteratee=_.identity] The function invoked per iteration. + * @returns {Object} Returns `object`. + * @example + * + * function Foo() { + * this.a = 1; + * this.b = 2; + * } + * + * Foo.prototype.c = 3; + * + * _.forOwn(new Foo, function(value, key) { + * console.log(key); + * }); + * // => logs 'a' then 'b' (iteration order is not guaranteed) + */ + function forOwn(object, iteratee) { + return object && baseForOwn(object, toFunction(iteratee)); } - function ensureAsync(fn) { - return rest(function (args) { - var callback = args.pop(); - var sync = true; - args.push(function () { - var innerArgs = arguments; - if (sync) { - setImmediate$1(function () { - callback.apply(null, innerArgs); - }); - } else { - callback.apply(null, innerArgs); - } - }); - fn.apply(this, args); - sync = false; - }); - } + /** + * Gets the index at which the first occurrence of `NaN` is found in `array`. + * + * @private + * @param {Array} array The array to search. + * @param {number} fromIndex The index to search from. + * @param {boolean} [fromRight] Specify iterating from right to left. + * @returns {number} Returns the index of the matched `NaN`, else `-1`. + */ + function indexOfNaN(array, fromIndex, fromRight) { + var length = array.length, + index = fromIndex + (fromRight ? 0 : -1); - function iterator (tasks) { - function makeCallback(index) { - function fn() { - if (tasks.length) { - tasks[index].apply(null, arguments); - } - return fn.next(); - } - fn.next = function () { - return index < tasks.length - 1 ? makeCallback(index + 1) : null; - }; - return fn; + while ((fromRight ? index-- : ++index < length)) { + var other = array[index]; + if (other !== other) { + return index; } - return makeCallback(0); + } + return -1; } - function waterfall (tasks, cb) { - cb = once(cb || noop); - if (!isArray(tasks)) return cb(new Error('First argument to waterfall must be an array of functions')); - if (!tasks.length) return cb(); + /** + * The base implementation of `_.indexOf` without `fromIndex` bounds checks. + * + * @private + * @param {Array} array The array to search. + * @param {*} value The value to search for. + * @param {number} fromIndex The index to search from. + * @returns {number} Returns the index of the matched value, else `-1`. + */ + function baseIndexOf(array, value, fromIndex) { + if (value !== value) { + return indexOfNaN(array, fromIndex); + } + var index = fromIndex - 1, + length = array.length; - function wrapIterator(iterator) { - return rest(function (err, args) { - if (err) { - cb.apply(null, [err].concat(args)); - } else { - var next = iterator.next(); - if (next) { - args.push(wrapIterator(next)); - } else { - args.push(cb); - } - ensureAsync(iterator).apply(null, args); - } - }); + while (++index < length) { + if (array[index] === value) { + return index; } - wrapIterator(iterator(tasks))(); + } + return -1; } - function until(test, iterator, cb) { - return whilst(function () { - return !test.apply(this, arguments); - }, iterator, cb); - } + /* Built-in method references for those with the same name as other `lodash` methods. */ + var nativeMax$1 = Math.max; - function unmemoize(fn) { - return function () { - return (fn.unmemoized || fn).apply(null, arguments); - }; + /** + * Gets the index at which the first occurrence of `value` is found in `array` + * using [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero) + * for equality comparisons. If `fromIndex` is negative, it's used as the offset + * from the end of `array`. If `array` is sorted providing `true` for `fromIndex` + * performs a faster binary search. + * + * @static + * @memberOf _ + * @category Array + * @param {Array} array The array to search. + * @param {*} value The value to search for. + * @param {number} [fromIndex=0] The index to search from. + * @returns {number} Returns the index of the matched value, else `-1`. + * @example + * + * _.indexOf([1, 2, 1, 2], 2); + * // => 1 + * + * // using `fromIndex` + * _.indexOf([1, 2, 1, 2], 2, 2); + * // => 3 + */ + function indexOf(array, value, fromIndex) { + var length = array ? array.length : 0; + if (!length) { + return -1; + } + fromIndex = toInteger(fromIndex); + if (fromIndex < 0) { + fromIndex = nativeMax$1(length + fromIndex, 0); + } + return baseIndexOf(array, value, fromIndex); } - function transform(arr, memo, iterator, callback) { - if (arguments.length === 3) { - callback = iterator; - iterator = memo; - memo = isArray(arr) ? [] : {}; + function auto (tasks, concurrency, callback) { + if (typeof arguments[1] === 'function') { + // concurrency is optional, shift the args. + callback = concurrency; + concurrency = null; + } + callback = once(callback || noop); + var keys$$ = keys(tasks); + var remainingTasks = keys$$.length; + if (!remainingTasks) { + return callback(null); + } + if (!concurrency) { + concurrency = remainingTasks; } - eachOf(arr, function (v, k, cb) { - iterator(memo, v, k, cb); - }, function (err) { - callback(err, memo); + var results = {}; + var runningTasks = 0; + + var listeners = []; + function addListener(fn) { + listeners.unshift(fn); + } + function removeListener(fn) { + var idx = indexOf(listeners, fn); + if (idx >= 0) listeners.splice(idx, 1); + } + function taskComplete() { + remainingTasks--; + arrayEach(listeners.slice(), function (fn) { + fn(); + }); + } + + addListener(function () { + if (!remainingTasks) { + callback(null, results); + } }); - } - function _asyncMap(eachfn, arr, iterator, callback) { - callback = once(callback || noop); - arr = arr || []; - var results = isArrayLike(arr) ? [] : {}; - eachfn(arr, function (value, index, callback) { - iterator(value, function (err, v) { - results[index] = v; - callback(err); + arrayEach(keys$$, function (k) { + var task = isArray(tasks[k]) ? tasks[k] : [tasks[k]]; + var taskCallback = rest(function (err, args) { + runningTasks--; + if (args.length <= 1) { + args = args[0]; + } + if (err) { + var safeResults = {}; + forOwn(results, function (val, rkey) { + safeResults[rkey] = val; + }); + safeResults[k] = args; + callback(err, safeResults); + } else { + results[k] = args; + setImmediate$1(taskComplete); + } }); - }, function (err) { - callback(err, results); + var requires = task.slice(0, task.length - 1); + // prevent dead-locks + var len = requires.length; + var dep; + while (len--) { + if (!(dep = tasks[requires[len]])) { + throw new Error('Has inexistant dependency'); + } + if (isArray(dep) && indexOf(dep, k) >= 0) { + throw new Error('Has cyclic dependencies'); + } + } + function ready() { + return runningTasks < concurrency && !baseHas(results, k) && arrayEvery(requires, function (x) { + return baseHas(results, x); + }); + } + if (ready()) { + runningTasks++; + task[task.length - 1](taskCallback, results); + } else { + addListener(listener); + } + function listener() { + if (ready()) { + runningTasks++; + removeListener(listener); + task[task.length - 1](taskCallback, results); + } + } }); } - var mapSeries = doSeries(_asyncMap); - - /* Built-in method references for those with the same name as other `lodash` methods. */ - var nativeCeil = Math.ceil; - var nativeMax$2 = Math.max; /** - * The base implementation of `_.range` and `_.rangeRight` which doesn't - * coerce arguments to numbers. + * A specialized version of `_.map` for arrays without support for iteratee + * shorthands. * * @private - * @param {number} start The start of the range. - * @param {number} end The end of the range. - * @param {number} step The value to increment or decrement by. - * @param {boolean} [fromRight] Specify iterating from right to left. - * @returns {Array} Returns the new array of numbers. + * @param {Array} array The array to iterate over. + * @param {Function} iteratee The function invoked per iteration. + * @returns {Array} Returns the new mapped array. */ - function baseRange(start, end, step, fromRight) { + function arrayMap(array, iteratee) { var index = -1, - length = nativeMax$2(nativeCeil((end - start) / (step || 1)), 0), + length = array.length, result = Array(length); - while (length--) { - result[fromRight ? length : ++index] = start; - start += step; + while (++index < length) { + result[index] = iteratee(array[index], index, array); } return result; } - function timesSeries (count, iterator, callback) { - mapSeries(baseRange(0, count, 1), iterator, callback); - } - - var mapLimit = doParallelLimit(_asyncMap); - - function timeLimit(count, limit, iterator, cb) { - return mapLimit(baseRange(0, count, 1), limit, iterator, cb); - } - - var map = doParallel(_asyncMap); + function queue$1(worker, concurrency, payload) { + if (concurrency == null) { + concurrency = 1; + } else if (concurrency === 0) { + throw new Error('Concurrency must not be zero'); + } + function _insert(q, data, pos, callback) { + if (callback != null && typeof callback !== 'function') { + throw new Error('task callback must be a function'); + } + q.started = true; + if (!isArray(data)) { + data = [data]; + } + if (data.length === 0 && q.idle()) { + // call drain immediately if there are no tasks + return setImmediate$1(function () { + q.drain(); + }); + } + arrayEach(data, function (task) { + var item = { + data: task, + callback: callback || noop + }; - function times (count, iterator, callback) { - map(baseRange(0, count, 1), iterator, callback); - } - - function sortBy(arr, iterator, cb) { - map(arr, function (x, cb) { - iterator(x, function (err, criteria) { - if (err) return cb(err); - cb(null, { value: x, criteria: criteria }); - }); - }, function (err, results) { - if (err) return cb(err); - cb(null, arrayMap(results.sort(comparator), baseProperty('value'))); - }); - - function comparator(left, right) { - var a = left.criteria, - b = right.criteria; - return a < b ? -1 : a > b ? 1 : 0; - } - } - - var someLimit = _createTester(eachOfLimit, Boolean, identity); - - function _parallel(eachfn, tasks, callback) { - callback = callback || noop; - var results = isArrayLike(tasks) ? [] : {}; - - eachfn(tasks, function (task, key, callback) { - task(rest(function (err, args) { - if (args.length <= 1) { - args = args[0]; - } - results[key] = args; - callback(err); - })); - }, function (err) { - callback(err, results); - }); - } - - function series(tasks, cb) { - return _parallel(eachOfSeries, tasks, cb); - } - - function seq() /* functions... */{ - var fns = arguments; - return rest(function (args) { - var that = this; - - var cb = args[args.length - 1]; - if (typeof cb == 'function') { - args.pop(); - } else { - cb = noop; - } - - reduce(fns, args, function (newargs, fn, cb) { - fn.apply(that, newargs.concat([rest(function (err, nextargs) { - cb(err, nextargs); - })])); - }, function (err, results) { - cb.apply(that, [err].concat(results)); - }); - }); - } - - function retry(times, task, callback) { - var DEFAULT_TIMES = 5; - var DEFAULT_INTERVAL = 0; - - var attempts = []; - - var opts = { - times: DEFAULT_TIMES, - interval: DEFAULT_INTERVAL - }; - - function parseTimes(acc, t) { - if (typeof t === 'number') { - acc.times = parseInt(t, 10) || DEFAULT_TIMES; - } else if (typeof t === 'object') { - acc.times = parseInt(t.times, 10) || DEFAULT_TIMES; - acc.interval = parseInt(t.interval, 10) || DEFAULT_INTERVAL; - } else { - throw new Error('Unsupported argument type for \'times\': ' + typeof t); - } - } - - var length = arguments.length; - if (length < 1 || length > 3) { - throw new Error('Invalid arguments - must be either (task), (task, callback), (times, task) or (times, task, callback)'); - } else if (length <= 2 && typeof times === 'function') { - callback = task; - task = times; - } - if (typeof times !== 'function') { - parseTimes(opts, times); - } - opts.callback = callback; - opts.task = task; - - function wrappedTask(wrappedCallback, wrappedResults) { - function retryAttempt(task, finalAttempt) { - return function (seriesCallback) { - task(function (err, result) { - seriesCallback(!err || finalAttempt, { - err: err, - result: result - }); - }, wrappedResults); - }; - } - - function retryInterval(interval) { - return function (seriesCallback) { - setTimeout(function () { - seriesCallback(null); - }, interval); - }; - } - - while (opts.times) { - - var finalAttempt = !(opts.times -= 1); - attempts.push(retryAttempt(opts.task, finalAttempt)); - if (!finalAttempt && opts.interval > 0) { - attempts.push(retryInterval(opts.interval)); - } - } - - series(attempts, function (done, data) { - data = data[data.length - 1]; - (wrappedCallback || opts.callback)(data.err, data.result); - }); - } - - // If a callback is passed, run this as a controll flow - return opts.callback ? wrappedTask() : wrappedTask; - } - - function reject$1(eachfn, arr, iterator, callback) { - _filter(eachfn, arr, function (value, cb) { - iterator(value, function (v) { - cb(!v); - }); - }, callback); - } - - var rejectSeries = doSeries(reject$1); - - var rejectLimit = doParallelLimit(reject$1); - - var reject = doParallel(reject$1); - - /** - * A specialized version of `_.forEach` for arrays without support for - * iteratee shorthands. - * - * @private - * @param {Array} array The array to iterate over. - * @param {Function} iteratee The function invoked per iteration. - * @returns {Array} Returns `array`. - */ - function arrayEach(array, iteratee) { - var index = -1, - length = array.length; - - while (++index < length) { - if (iteratee(array[index], index, array) === false) { - break; - } - } - return array; - } - - function queue$1(worker, concurrency, payload) { - if (concurrency == null) { - concurrency = 1; - } else if (concurrency === 0) { - throw new Error('Concurrency must not be zero'); - } - function _insert(q, data, pos, callback) { - if (callback != null && typeof callback !== 'function') { - throw new Error('task callback must be a function'); - } - q.started = true; - if (!isArray(data)) { - data = [data]; - } - if (data.length === 0 && q.idle()) { - // call drain immediately if there are no tasks - return setImmediate$1(function () { - q.drain(); - }); - } - arrayEach(data, function (task) { - var item = { - data: task, - callback: callback || noop - }; - - if (pos) { - q.tasks.unshift(item); - } else { - q.tasks.push(item); - } + if (pos) { + q.tasks.unshift(item); + } else { + q.tasks.push(item); + } if (q.tasks.length === q.concurrency) { q.saturated(); @@ -1488,115 +1345,164 @@ return q; } - function queue (worker, concurrency) { - return queue$1(function (items, cb) { - worker(items[0], cb); - }, concurrency, 1); + function cargo(worker, payload) { + return queue$1(worker, 1, payload); } - function priorityQueue (worker, concurrency) { - function _compareTasks(a, b) { - return a.priority - b.priority; - } + function reduce(arr, memo, iterator, cb) { + eachOfSeries(arr, function (x, i, cb) { + iterator(memo, x, function (err, v) { + memo = v; + cb(err); + }); + }, function (err) { + cb(err, memo); + }); + } - function _binarySearch(sequence, item, compare) { - var beg = -1, - end = sequence.length - 1; - while (beg < end) { - var mid = beg + (end - beg + 1 >>> 1); - if (compare(item, sequence[mid]) >= 0) { - beg = mid; - } else { - end = mid - 1; - } - } - return beg; - } + function seq() /* functions... */{ + var fns = arguments; + return rest(function (args) { + var that = this; - function _insert(q, data, priority, callback) { - if (callback != null && typeof callback !== 'function') { - throw new Error('task callback must be a function'); - } - q.started = true; - if (!isArray(data)) { - data = [data]; + var cb = args[args.length - 1]; + if (typeof cb == 'function') { + args.pop(); + } else { + cb = noop; } - if (data.length === 0) { - // call drain immediately if there are no tasks - return setImmediate$1(function () { - q.drain(); - }); - } - arrayEach(data, function (task) { - var item = { - data: task, - priority: priority, - callback: typeof callback === 'function' ? callback : noop - }; - q.tasks.splice(_binarySearch(q.tasks, item, _compareTasks) + 1, 0, item); - - if (q.tasks.length === q.concurrency) { - q.saturated(); - } - setImmediate$1(q.process); + reduce(fns, args, function (newargs, fn, cb) { + fn.apply(that, newargs.concat([rest(function (err, nextargs) { + cb(err, nextargs); + })])); + }, function (err, results) { + cb.apply(that, [err].concat(results)); }); - } - - // Start with a normal queue - var q = queue(worker, concurrency); + }); + } - // Override push to accept second parameter representing priority - q.push = function (data, priority, callback) { - _insert(q, data, priority, callback); - }; + var reverse = Array.prototype.reverse; - // Remove unshift function - delete q.unshift; + function compose() /* functions... */{ + return seq.apply(null, reverse.call(arguments)); + } - return q; + function concat$1(eachfn, arr, fn, callback) { + var result = []; + eachfn(arr, function (x, index, cb) { + fn(x, function (err, y) { + result = result.concat(y || []); + cb(err); + }); + }, function (err) { + callback(err, result); + }); } - function parallelLimit(tasks, limit, cb) { - return _parallel(_eachOfLimit(limit), tasks, cb); + function doParallel(fn) { + return function (obj, iterator, callback) { + return fn(eachOf, obj, iterator, callback); + }; } - function parallel(tasks, cb) { - return _parallel(eachOf, tasks, cb); + var concat = doParallel(concat$1); + + function doSeries(fn) { + return function (obj, iterator, callback) { + return fn(eachOfSeries, obj, iterator, callback); + }; } - var nexTick = typeof process === 'object' && typeof process.nextTick === 'function' ? process.nextTick : setImmediate$1; + var concatSeries = doSeries(concat$1); - function memoize(fn, hasher) { - var memo = {}; - var queues = {}; - hasher = hasher || identity; - var memoized = rest(function memoized(args) { - var callback = args.pop(); - var key = hasher.apply(null, args); - if (key in memo) { - setImmediate$1(function () { - callback.apply(null, memo[key]); + var constant = rest(function (values) { + var args = [null].concat(values); + return function (cb) { + return cb.apply(this, args); + }; + }); + + function _createTester(eachfn, check, getResult) { + return function (arr, limit, iterator, cb) { + function done() { + if (cb) cb(getResult(false, void 0)); + } + function iteratee(x, _, callback) { + if (!cb) return callback(); + iterator(x, function (v) { + if (cb && check(v)) { + cb(getResult(true, x)); + cb = iterator = false; + } + callback(); }); - } else if (key in queues) { - queues[key].push(callback); + } + if (arguments.length > 3) { + eachfn(arr, limit, iteratee, done); } else { - queues[key] = [callback]; - fn.apply(null, args.concat([rest(function (args) { - memo[key] = args; - var q = queues[key]; - delete queues[key]; - for (var i = 0, l = q.length; i < l; i++) { - q[i].apply(null, args); - } - })])); + cb = iterator; + iterator = limit; + eachfn(arr, iteratee, done); } - }); - memoized.memo = memo; - memoized.unmemoized = fn; - return memoized; + }; + } + + function _findGetResult(v, x) { + return x; + } + + var detect = _createTester(eachOf, identity, _findGetResult); + + function _eachOfLimit(limit) { + return function (obj, iterator, callback) { + callback = once(callback || noop); + obj = obj || []; + var nextKey = keyIterator(obj); + if (limit <= 0) { + return callback(null); + } + var done = false; + var running = 0; + var errored = false; + + (function replenish() { + if (done && running <= 0) { + return callback(null); + } + + while (running < limit && !errored) { + var key = nextKey(); + if (key === null) { + done = true; + if (running <= 0) { + callback(null); + } + return; + } + running += 1; + iterator(obj[key], key, onlyOnce(function (err) { + running -= 1; + if (err) { + callback(err); + errored = true; + } else { + replenish(); + } + })); + } + })(); + }; + } + + function eachOfLimit(obj, limit, iterator, cb) { + _eachOfLimit(limit)(obj, iterator, cb); } + var detectLimit = _createTester(eachOfLimit, identity, _findGetResult); + + var detectSeries = _createTester(eachOfSeries, identity, _findGetResult); + function consoleFunc(name) { return rest(function (fn, args) { fn.apply(null, args.concat([rest(function (err, args) { @@ -1615,20 +1521,7 @@ }); } - var log = consoleFunc('log'); - - function forever(fn, cb) { - var done = onlyOnce(cb || noop); - var task = ensureAsync(fn); - - function next(err) { - if (err) return done(err); - task(next); - } - next(); - } - - var everyLimit = _createTester(eachOfLimit, notId, notId); + var dir = consoleFunc('dir'); function during(test, iterator, cb) { cb = cb || noop; @@ -1651,6 +1544,26 @@ test(check); } + function doDuring(iterator, test, cb) { + var calls = 0; + + during(function (next) { + if (calls++ < 1) return next(null, true); + test.apply(this, arguments); + }, iterator, cb); + } + + function whilst(test, iterator, cb) { + cb = cb || noop; + if (!test()) return cb(null); + var next = rest(function (err, args) { + if (err) return cb(err); + if (test.apply(this, args)) return iterator(next); + cb.apply(null, [null].concat(args)); + }); + iterator(next); + } + function doWhilst(iterator, test, cb) { var calls = 0; return whilst(function () { @@ -1664,374 +1577,461 @@ }, cb); } - function doDuring(iterator, test, cb) { - var calls = 0; + function _withoutIndex(iterator) { + return function (value, index, callback) { + return iterator(value, callback); + }; + } - during(function (next) { - if (calls++ < 1) return next(null, true); - test.apply(this, arguments); - }, iterator, cb); + function each(arr, iterator, cb) { + return eachOf(arr, _withoutIndex(iterator), cb); } - var dir = consoleFunc('dir'); + function eachLimit(arr, limit, iterator, cb) { + return _eachOfLimit(limit)(arr, _withoutIndex(iterator), cb); + } - function _findGetResult(v, x) { - return x; + function eachSeries(arr, iterator, cb) { + return eachOfSeries(arr, _withoutIndex(iterator), cb); } - var detectSeries = _createTester(eachOfSeries, identity, _findGetResult); + function ensureAsync(fn) { + return rest(function (args) { + var callback = args.pop(); + var sync = true; + args.push(function () { + var innerArgs = arguments; + if (sync) { + setImmediate$1(function () { + callback.apply(null, innerArgs); + }); + } else { + callback.apply(null, innerArgs); + } + }); + fn.apply(this, args); + sync = false; + }); + } - var detectLimit = _createTester(eachOfLimit, identity, _findGetResult); + function notId(v) { + return !v; + } - var detect = _createTester(eachOf, identity, _findGetResult); + var every = _createTester(eachOf, notId, notId); - var constant = rest(function (values) { - var args = [null].concat(values); - return function (cb) { - return cb.apply(this, args); - }; - }); + var everyLimit = _createTester(eachOfLimit, notId, notId); - function concat$1(eachfn, arr, fn, callback) { - var result = []; - eachfn(arr, function (x, index, cb) { - fn(x, function (err, y) { - result = result.concat(y || []); - cb(err); + function _filter(eachfn, arr, iterator, callback) { + var results = []; + eachfn(arr, function (x, index, callback) { + iterator(x, function (v) { + if (v) { + results.push({ index: index, value: x }); + } + callback(); }); - }, function (err) { - callback(err, result); + }, function () { + callback(arrayMap(results.sort(function (a, b) { + return a.index - b.index; + }), baseProperty('value'))); }); } - var concatSeries = doSeries(concat$1); + var filter = doParallel(_filter); - var concat = doParallel(concat$1); + function doParallelLimit(fn) { + return function (obj, limit, iterator, callback) { + return fn(_eachOfLimit(limit), obj, iterator, callback); + }; + } - var reverse = Array.prototype.reverse; + var filterLimit = doParallelLimit(_filter); - function compose() /* functions... */{ - return seq.apply(null, reverse.call(arguments)); + var filterSeries = doSeries(_filter); + + function forever(fn, cb) { + var done = onlyOnce(cb || noop); + var task = ensureAsync(fn); + + function next(err) { + if (err) return done(err); + task(next); + } + next(); } - function cargo(worker, payload) { - return queue$1(worker, 1, payload); + function iterator (tasks) { + function makeCallback(index) { + function fn() { + if (tasks.length) { + tasks[index].apply(null, arguments); + } + return fn.next(); + } + fn.next = function () { + return index < tasks.length - 1 ? makeCallback(index + 1) : null; + }; + return fn; + } + return makeCallback(0); } - /** - * A specialized version of `_.every` for arrays without support for - * iteratee shorthands. - * - * @private - * @param {Array} array The array to iterate over. - * @param {Function} predicate The function invoked per iteration. - * @returns {boolean} Returns `true` if all elements pass the predicate check, else `false`. - */ - function arrayEvery(array, predicate) { - var index = -1, - length = array.length; + var log = consoleFunc('log'); - while (++index < length) { - if (!predicate(array[index], index, array)) { - return false; - } - } - return true; + function _asyncMap(eachfn, arr, iterator, callback) { + callback = once(callback || noop); + arr = arr || []; + var results = isArrayLike(arr) ? [] : {}; + eachfn(arr, function (value, index, callback) { + iterator(value, function (err, v) { + results[index] = v; + callback(err); + }); + }, function (err) { + callback(err, results); + }); } - /** - * Creates a base function for methods like `_.forIn`. - * - * @private - * @param {boolean} [fromRight] Specify iterating from right to left. - * @returns {Function} Returns the new base function. - */ - function createBaseFor(fromRight) { - return function(object, iteratee, keysFunc) { - var index = -1, - iterable = Object(object), - props = keysFunc(object), - length = props.length; + var map = doParallel(_asyncMap); - while (length--) { - var key = props[fromRight ? length : ++index]; - if (iteratee(iterable[key], key, iterable) === false) { - break; - } - } - return object; - }; + var mapLimit = doParallelLimit(_asyncMap); + + var mapSeries = doSeries(_asyncMap); + + function memoize(fn, hasher) { + var memo = {}; + var queues = {}; + hasher = hasher || identity; + var memoized = rest(function memoized(args) { + var callback = args.pop(); + var key = hasher.apply(null, args); + if (key in memo) { + setImmediate$1(function () { + callback.apply(null, memo[key]); + }); + } else if (key in queues) { + queues[key].push(callback); + } else { + queues[key] = [callback]; + fn.apply(null, args.concat([rest(function (args) { + memo[key] = args; + var q = queues[key]; + delete queues[key]; + for (var i = 0, l = q.length; i < l; i++) { + q[i].apply(null, args); + } + })])); + } + }); + memoized.memo = memo; + memoized.unmemoized = fn; + return memoized; } - /** - * The base implementation of `baseForIn` and `baseForOwn` which iterates - * over `object` properties returned by `keysFunc` invoking `iteratee` for - * each property. Iteratee functions may exit iteration early by explicitly - * returning `false`. - * - * @private - * @param {Object} object The object to iterate over. - * @param {Function} iteratee The function invoked per iteration. - * @param {Function} keysFunc The function to get the keys of `object`. - * @returns {Object} Returns `object`. - */ - var baseFor = createBaseFor(); + var nexTick = typeof process === 'object' && typeof process.nextTick === 'function' ? process.nextTick : setImmediate$1; - /** - * The base implementation of `_.forOwn` without support for iteratee shorthands. - * - * @private - * @param {Object} object The object to iterate over. - * @param {Function} iteratee The function invoked per iteration. - * @returns {Object} Returns `object`. - */ - function baseForOwn(object, iteratee) { - return object && baseFor(object, iteratee, keys); + function _parallel(eachfn, tasks, callback) { + callback = callback || noop; + var results = isArrayLike(tasks) ? [] : {}; + + eachfn(tasks, function (task, key, callback) { + task(rest(function (err, args) { + if (args.length <= 1) { + args = args[0]; + } + results[key] = args; + callback(err); + })); + }, function (err) { + callback(err, results); + }); } - /** - * Converts `value` to a function if it's not one. - * - * @private - * @param {*} value The value to process. - * @returns {Function} Returns the function. - */ - function toFunction(value) { - return typeof value == 'function' ? value : identity; + function parallel(tasks, cb) { + return _parallel(eachOf, tasks, cb); } - /** - * Iterates over own enumerable properties of an object invoking `iteratee` - * for each property. The iteratee is invoked with three arguments: - * (value, key, object). Iteratee functions may exit iteration early by - * explicitly returning `false`. - * - * @static - * @memberOf _ - * @category Object - * @param {Object} object The object to iterate over. - * @param {Function} [iteratee=_.identity] The function invoked per iteration. - * @returns {Object} Returns `object`. - * @example - * - * function Foo() { - * this.a = 1; - * this.b = 2; - * } - * - * Foo.prototype.c = 3; - * - * _.forOwn(new Foo, function(value, key) { - * console.log(key); - * }); - * // => logs 'a' then 'b' (iteration order is not guaranteed) - */ - function forOwn(object, iteratee) { - return object && baseForOwn(object, toFunction(iteratee)); + function parallelLimit(tasks, limit, cb) { + return _parallel(_eachOfLimit(limit), tasks, cb); } - /** - * Gets the index at which the first occurrence of `NaN` is found in `array`. - * - * @private - * @param {Array} array The array to search. - * @param {number} fromIndex The index to search from. - * @param {boolean} [fromRight] Specify iterating from right to left. - * @returns {number} Returns the index of the matched `NaN`, else `-1`. - */ - function indexOfNaN(array, fromIndex, fromRight) { - var length = array.length, - index = fromIndex + (fromRight ? 0 : -1); + function queue (worker, concurrency) { + return queue$1(function (items, cb) { + worker(items[0], cb); + }, concurrency, 1); + } - while ((fromRight ? index-- : ++index < length)) { - var other = array[index]; - if (other !== other) { - return index; + function priorityQueue (worker, concurrency) { + function _compareTasks(a, b) { + return a.priority - b.priority; } - } - return -1; - } - /** - * The base implementation of `_.indexOf` without `fromIndex` bounds checks. - * - * @private - * @param {Array} array The array to search. - * @param {*} value The value to search for. - * @param {number} fromIndex The index to search from. - * @returns {number} Returns the index of the matched value, else `-1`. - */ - function baseIndexOf(array, value, fromIndex) { - if (value !== value) { - return indexOfNaN(array, fromIndex); - } - var index = fromIndex - 1, - length = array.length; + function _binarySearch(sequence, item, compare) { + var beg = -1, + end = sequence.length - 1; + while (beg < end) { + var mid = beg + (end - beg + 1 >>> 1); + if (compare(item, sequence[mid]) >= 0) { + beg = mid; + } else { + end = mid - 1; + } + } + return beg; + } - while (++index < length) { - if (array[index] === value) { - return index; + function _insert(q, data, priority, callback) { + if (callback != null && typeof callback !== 'function') { + throw new Error('task callback must be a function'); + } + q.started = true; + if (!isArray(data)) { + data = [data]; + } + if (data.length === 0) { + // call drain immediately if there are no tasks + return setImmediate$1(function () { + q.drain(); + }); + } + arrayEach(data, function (task) { + var item = { + data: task, + priority: priority, + callback: typeof callback === 'function' ? callback : noop + }; + + q.tasks.splice(_binarySearch(q.tasks, item, _compareTasks) + 1, 0, item); + + if (q.tasks.length === q.concurrency) { + q.saturated(); + } + setImmediate$1(q.process); + }); } - } - return -1; + + // Start with a normal queue + var q = queue(worker, concurrency); + + // Override push to accept second parameter representing priority + q.push = function (data, priority, callback) { + _insert(q, data, priority, callback); + }; + + // Remove unshift function + delete q.unshift; + + return q; } - /* Built-in method references for those with the same name as other `lodash` methods. */ - var nativeMax$1 = Math.max; + var slice = Array.prototype.slice; - /** - * Gets the index at which the first occurrence of `value` is found in `array` - * using [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero) - * for equality comparisons. If `fromIndex` is negative, it's used as the offset - * from the end of `array`. If `array` is sorted providing `true` for `fromIndex` - * performs a faster binary search. - * - * @static - * @memberOf _ - * @category Array - * @param {Array} array The array to search. - * @param {*} value The value to search for. - * @param {number} [fromIndex=0] The index to search from. - * @returns {number} Returns the index of the matched value, else `-1`. - * @example - * - * _.indexOf([1, 2, 1, 2], 2); - * // => 1 - * - * // using `fromIndex` - * _.indexOf([1, 2, 1, 2], 2, 2); - * // => 3 - */ - function indexOf(array, value, fromIndex) { - var length = array ? array.length : 0; - if (!length) { - return -1; - } - fromIndex = toInteger(fromIndex); - if (fromIndex < 0) { - fromIndex = nativeMax$1(length + fromIndex, 0); - } - return baseIndexOf(array, value, fromIndex); + function reduceRight(arr, memo, iterator, cb) { + var reversed = slice.call(arr).reverse(); + reduce(reversed, memo, iterator, cb); } - function auto (tasks, concurrency, callback) { - if (typeof arguments[1] === 'function') { - // concurrency is optional, shift the args. - callback = concurrency; - concurrency = null; - } - callback = once(callback || noop); - var keys$$ = keys(tasks); - var remainingTasks = keys$$.length; - if (!remainingTasks) { - return callback(null); - } - if (!concurrency) { - concurrency = remainingTasks; - } + function reject$1(eachfn, arr, iterator, callback) { + _filter(eachfn, arr, function (value, cb) { + iterator(value, function (v) { + cb(!v); + }); + }, callback); + } - var results = {}; - var runningTasks = 0; + var reject = doParallel(reject$1); - var listeners = []; - function addListener(fn) { - listeners.unshift(fn); + var rejectLimit = doParallelLimit(reject$1); + + var rejectSeries = doSeries(reject$1); + + function series(tasks, cb) { + return _parallel(eachOfSeries, tasks, cb); + } + + function retry(times, task, callback) { + var DEFAULT_TIMES = 5; + var DEFAULT_INTERVAL = 0; + + var attempts = []; + + var opts = { + times: DEFAULT_TIMES, + interval: DEFAULT_INTERVAL + }; + + function parseTimes(acc, t) { + if (typeof t === 'number') { + acc.times = parseInt(t, 10) || DEFAULT_TIMES; + } else if (typeof t === 'object') { + acc.times = parseInt(t.times, 10) || DEFAULT_TIMES; + acc.interval = parseInt(t.interval, 10) || DEFAULT_INTERVAL; + } else { + throw new Error('Unsupported argument type for \'times\': ' + typeof t); + } } - function removeListener(fn) { - var idx = indexOf(listeners, fn); - if (idx >= 0) listeners.splice(idx, 1); + + var length = arguments.length; + if (length < 1 || length > 3) { + throw new Error('Invalid arguments - must be either (task), (task, callback), (times, task) or (times, task, callback)'); + } else if (length <= 2 && typeof times === 'function') { + callback = task; + task = times; } - function taskComplete() { - remainingTasks--; - arrayEach(listeners.slice(), function (fn) { - fn(); - }); + if (typeof times !== 'function') { + parseTimes(opts, times); } + opts.callback = callback; + opts.task = task; - addListener(function () { - if (!remainingTasks) { - callback(null, results); + function wrappedTask(wrappedCallback, wrappedResults) { + function retryAttempt(task, finalAttempt) { + return function (seriesCallback) { + task(function (err, result) { + seriesCallback(!err || finalAttempt, { + err: err, + result: result + }); + }, wrappedResults); + }; } - }); - arrayEach(keys$$, function (k) { - var task = isArray(tasks[k]) ? tasks[k] : [tasks[k]]; - var taskCallback = rest(function (err, args) { - runningTasks--; - if (args.length <= 1) { - args = args[0]; - } - if (err) { - var safeResults = {}; - forOwn(results, function (val, rkey) { - safeResults[rkey] = val; - }); - safeResults[k] = args; - callback(err, safeResults); - } else { - results[k] = args; - setImmediate$1(taskComplete); - } - }); - var requires = task.slice(0, task.length - 1); - // prevent dead-locks - var len = requires.length; - var dep; - while (len--) { - if (!(dep = tasks[requires[len]])) { - throw new Error('Has inexistant dependency'); - } - if (isArray(dep) && indexOf(dep, k) >= 0) { - throw new Error('Has cyclic dependencies'); - } - } - function ready() { - return runningTasks < concurrency && !baseHas(results, k) && arrayEvery(requires, function (x) { - return baseHas(results, x); - }); - } - if (ready()) { - runningTasks++; - task[task.length - 1](taskCallback, results); - } else { - addListener(listener); + function retryInterval(interval) { + return function (seriesCallback) { + setTimeout(function () { + seriesCallback(null); + }, interval); + }; } - function listener() { - if (ready()) { - runningTasks++; - removeListener(listener); - task[task.length - 1](taskCallback, results); + + while (opts.times) { + + var finalAttempt = !(opts.times -= 1); + attempts.push(retryAttempt(opts.task, finalAttempt)); + if (!finalAttempt && opts.interval > 0) { + attempts.push(retryInterval(opts.interval)); } } - }); + + series(attempts, function (done, data) { + data = data[data.length - 1]; + (wrappedCallback || opts.callback)(data.err, data.result); + }); + } + + // If a callback is passed, run this as a controll flow + return opts.callback ? wrappedTask() : wrappedTask; } - var apply = rest(function (fn, args) { - return rest(function (callArgs) { - return fn.apply(null, args.concat(callArgs)); - }); - }); + var some = _createTester(eachOf, Boolean, identity); - function applyEach$1(eachfn) { - return rest(function (fns, args) { - var go = rest(function (args) { - var that = this; - var callback = args.pop(); - return eachfn(fns, function (fn, _, cb) { - fn.apply(that, args.concat([cb])); - }, callback); + var someLimit = _createTester(eachOfLimit, Boolean, identity); + + function sortBy(arr, iterator, cb) { + map(arr, function (x, cb) { + iterator(x, function (err, criteria) { + if (err) return cb(err); + cb(null, { value: x, criteria: criteria }); }); - if (args.length) { - return go.apply(this, args); - } else { - return go; - } + }, function (err, results) { + if (err) return cb(err); + cb(null, arrayMap(results.sort(comparator), baseProperty('value'))); }); + + function comparator(left, right) { + var a = left.criteria, + b = right.criteria; + return a < b ? -1 : a > b ? 1 : 0; + } } - var applyEachSeries = applyEach$1(eachOfSeries); + /* Built-in method references for those with the same name as other `lodash` methods. */ + var nativeCeil = Math.ceil; + var nativeMax$2 = Math.max; + /** + * The base implementation of `_.range` and `_.rangeRight` which doesn't + * coerce arguments to numbers. + * + * @private + * @param {number} start The start of the range. + * @param {number} end The end of the range. + * @param {number} step The value to increment or decrement by. + * @param {boolean} [fromRight] Specify iterating from right to left. + * @returns {Array} Returns the new array of numbers. + */ + function baseRange(start, end, step, fromRight) { + var index = -1, + length = nativeMax$2(nativeCeil((end - start) / (step || 1)), 0), + result = Array(length); - var applyEach = applyEach$1(eachOf); + while (length--) { + result[fromRight ? length : ++index] = start; + start += step; + } + return result; + } + + function times (count, iterator, callback) { + map(baseRange(0, count, 1), iterator, callback); + } + + function timeLimit(count, limit, iterator, cb) { + return mapLimit(baseRange(0, count, 1), limit, iterator, cb); + } + + function timesSeries (count, iterator, callback) { + mapSeries(baseRange(0, count, 1), iterator, callback); + } + + function transform(arr, memo, iterator, callback) { + if (arguments.length === 3) { + callback = iterator; + iterator = memo; + memo = isArray(arr) ? [] : {}; + } + + eachOf(arr, function (v, k, cb) { + iterator(memo, v, k, cb); + }, function (err) { + callback(err, memo); + }); + } + + function unmemoize(fn) { + return function () { + return (fn.unmemoized || fn).apply(null, arguments); + }; + } + + function until(test, iterator, cb) { + return whilst(function () { + return !test.apply(this, arguments); + }, iterator, cb); + } + + function waterfall (tasks, cb) { + cb = once(cb || noop); + if (!isArray(tasks)) return cb(new Error('First argument to waterfall must be an array of functions')); + if (!tasks.length) return cb(); + + function wrapIterator(iterator) { + return rest(function (err, args) { + if (err) { + cb.apply(null, [err].concat(args)); + } else { + var next = iterator.next(); + if (next) { + args.push(wrapIterator(next)); + } else { + args.push(cb); + } + ensureAsync(iterator).apply(null, args); + } + }); + } + wrapIterator(iterator(tasks))(); + } var index = { applyEach: applyEach, diff --git a/build/async.min.js b/build/async.min.js index 9eb834601..ff40154df 100644 --- a/build/async.min.js +++ b/build/async.min.js @@ -1,2 +1,2 @@ -!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(n.async={})}(this,function(n){"use strict";function t(n){var t=typeof n;return!!n&&("object"==t||"function"==t)}function r(n,t,r){var e=r?r.length:0;switch(e){case 0:return n.call(t);case 1:return n.call(t,r[0]);case 2:return n.call(t,r[0],r[1]);case 3:return n.call(t,r[0],r[1],r[2])}return n.apply(t,r)}function e(n){var r=t(n)?Hn.call(n):"";return r==Un||r==Cn}function u(n){if(t(n)){var r=e(n.valueOf)?n.valueOf():n;n=t(r)?r+"":r}if("string"!=typeof n)return 0===n?n:+n;n=n.replace(Qn,"");var u=Wn.test(n);return u||Gn.test(n)?Jn(n.slice(2),u?2:8):Rn.test(n)?Nn:+n}function i(n){if(!n)return 0===n?n:0;if(n=u(n),n===Kn||n===-Kn){var t=0>n?-1:1;return t*Vn}var r=n%1;return n===n?r?n-r:n:0}function o(n,t){if("function"!=typeof n)throw new TypeError(Xn);return t=Yn(void 0===t?n.length-1:i(t),0),function(){for(var e=arguments,u=-1,i=Yn(e.length-t,0),o=Array(i);++u0&&(r=t.apply(this,arguments)),1>=n&&(t=void 0),r}}function p(n){return s(2,n)}function h(){}function y(n){return"number"==typeof n&&n>-1&&n%1==0&&nt>=n}function m(n){return null!=n&&!("function"==typeof n&&e(n))&&y(_n(n))}function v(n,t){return rt.call(n,t)||"object"==typeof n&&t in n&&null===et(n)}function d(n){return ut(Object(n))}function g(n,t){for(var r=-1,e=Array(n);++r-1&&n%1==0&&t>n}function L(n){var t=n&&n.constructor,r="function"==typeof t&&t.prototype||vt;return n===r}function O(n){var t=L(n);if(!t&&!m(n))return d(n);var r=S(n),e=!!r,u=r||[],i=u.length;for(var o in n)!v(n,o)||e&&("length"==o||j(o,i))||t&&"constructor"==o||u.push(o);return u}function x(n){var t,r=-1;if(m(n))return t=n.length,function(){return r++,t>r?r:null};var e=O(n);return t=e.length,function(){return r++,t>r?e[r]:null}}function A(n){return function(){if(null===n)throw new Error("Callback was already called.");n.apply(this,arguments),n=null}}function I(n,t,r){function e(){var o=!0;return null===i?r(null):(t(n[i],i,A(function(n){if(n)r(n);else{if(i=u(),null===i)return r(null);o?gt(e):e()}})),void(o=!1))}r=p(r||h),n=n||[];var u=x(n),i=u();e()}function T(n){return function(t,r,e){return n(I,t,r,e)}}function z(n){return function(t,r,e){e=p(e||h),t=t||[];var u=x(t);if(0>=n)return e(null);var i=!1,o=0,c=!1;!function a(){if(i&&0>=o)return e(null);for(;n>o&&!c;){var f=u();if(null===f)return i=!0,void(0>=o&&e(null));o+=1,r(t[f],f,A(function(n){o-=1,n?(e(n),c=!0):a()}))}}()}}function M(n){return function(t,r,e,u){return n(z(r),t,e,u)}}function $(n,t,r){function e(n){o--,n?r(n):null===u&&0>=o&&r(null)}r=p(r||h),n=n||[];for(var u,i=x(n),o=0;null!=(u=i());)o+=1,t(n[u],u,A(e));0===o&&r(null)}function q(n){return function(t,r,e){return n($,t,r,e)}}function B(n,t,r,e){I(n,function(n,e,u){r(t,n,function(n,r){t=r,u(n)})},function(n){e(n,t)})}function F(n,t,r,e){var u=Et.call(n).reverse();B(u,t,r,e)}function P(n,t,r,e){z(t)(n,r,e)}function U(n){return function(t,r,e){return n(t,e)}}function C(n,t,r,e){return z(t)(n,U(r),e)}function D(n,t,r){return I(n,U(t),r)}function H(n,t,r){return $(n,U(t),r)}function N(n){return n}function Q(n,t,r){return function(e,u,i,o){function c(){o&&o(r(!1,void 0))}function a(n,e,u){return o?void i(n,function(e){o&&t(e)&&(o(r(!0,n)),o=i=!1),u()}):u()}arguments.length>3?n(e,u,a,c):(o=i,i=u,n(e,a,c))}}function R(n){return!n}function W(n,t,r){if(r=r||h,!n())return r(null);var e=o(function(u,i){return u?r(u):n.apply(this,i)?t(e):void r.apply(null,[null].concat(i))});t(e)}function G(n){return o(function(t){var r=t.pop(),e=!0;t.push(function(){var n=arguments;e?gt(function(){r.apply(null,n)}):r.apply(null,n)}),n.apply(this,t),e=!1})}function J(n){function t(r){function e(){return n.length&&n[r].apply(null,arguments),e.next()}return e.next=function(){return rr?-1:r>e?1:0}It(n,function(n,r){t(n,function(t,e){return t?r(t):void r(null,{value:n,criteria:e})})},function(n,t){return n?r(n):void r(null,a(t.sort(e),f("value")))})}function un(n,t,r){r=r||h;var e=m(t)?[]:{};n(t,function(n,t,r){n(o(function(n,u){u.length<=1&&(u=u[0]),e[t]=u,r(n)}))},function(n){r(n,e)})}function on(n,t){return un(I,n,t)}function cn(){var n=arguments;return o(function(t){var r=this,e=t[t.length-1];"function"==typeof e?t.pop():e=h,B(n,t,function(n,t,e){t.apply(r,n.concat([o(function(n,t){e(n,t)})]))},function(n,t){e.apply(r,[n].concat(t))})})}function an(n,t,r){function e(n,t){if("number"==typeof t)n.times=parseInt(t,10)||i;else{if("object"!=typeof t)throw new Error("Unsupported argument type for 'times': "+typeof t);n.times=parseInt(t.times,10)||i,n.interval=parseInt(t.interval,10)||o}}function u(n,t){function r(n,r){return function(e){n(function(n,t){e(!n||r,{err:n,result:t})},t)}}function e(n){return function(t){setTimeout(function(){t(null)},n)}}for(;a.times;){var u=!(a.times-=1);c.push(r(a.task,u)),!u&&a.interval>0&&c.push(e(a.interval))}on(c,function(t,r){r=r[r.length-1],(n||a.callback)(r.err,r.result)})}var i=5,o=0,c=[],a={times:i,interval:o},f=arguments.length;if(1>f||f>3)throw new Error("Invalid arguments - must be either (task), (task, callback), (times, task) or (times, task, callback)");return 2>=f&&"function"==typeof n&&(r=t,t=n),"function"!=typeof n&&e(a,n),a.callback=r,a.task=t,a.callback?u():u}function fn(n,t,r,e){l(n,t,function(n,t){r(n,function(n){t(!n)})},e)}function ln(n,t){for(var r=-1,e=n.length;++r=t;t++)gt(c.process)}}};return c}function pn(n,t){return sn(function(t,r){n(t[0],r)},t,1)}function hn(n,t){function r(n,t){return n.priority-t.priority}function e(n,t,r){for(var e=-1,u=n.length-1;u>e;){var i=e+(u-e+1>>>1);r(t,n[i])>=0?e=i:u=i-1}return e}function u(n,t,u,i){if(null!=i&&"function"!=typeof i)throw new Error("task callback must be a function");return n.started=!0,lt(t)||(t=[t]),0===t.length?gt(function(){n.drain()}):void ln(t,function(t){var o={data:t,priority:u,callback:"function"==typeof i?i:h};n.tasks.splice(e(n.tasks,o,r)+1,0,o),n.tasks.length===n.concurrency&&n.saturated(),gt(n.process)})}var i=pn(n,t);return i.push=function(n,t,r){u(i,n,t,r)},delete i.unshift,i}function yn(n,t,r){return un(z(t),n,r)}function mn(n,t){return un($,n,t)}function vn(n,t){var r={},e={};t=t||N;var u=o(function(u){var i=u.pop(),c=t.apply(null,u);c in r?gt(function(){i.apply(null,r[c])}):c in e?e[c].push(i):(e[c]=[i],n.apply(null,u.concat([o(function(n){r[c]=n;var t=e[c];delete e[c];for(var u=0,i=t.length;i>u;u++)t[u].apply(null,n)})])))});return u.memo=r,u.unmemoized=n,u}function dn(n){return o(function(t,r){t.apply(null,r.concat([o(function(t,r){"object"==typeof console&&(t?console.error&&console.error(t):console[n]&&ln(r,function(t){console[n](t)}))})]))})}function gn(n,t){function r(n){return n?e(n):void u(r)}var e=A(t||h),u=G(n);r()}function kn(n,t,r){r=r||h;var e=o(function(t,e){t?r(t):(e.push(u),n.apply(this,e))}),u=function(n,u){return n?r(n):u?void t(e):r(null)};n(u)}function bn(n,t,r){var e=0;return W(function(){return++e<=1||t.apply(this,arguments)},n,r)}function wn(n,t,r){return bn(n,function(){return!t.apply(this,arguments)},r)}function En(n,t,r){var e=0;kn(function(n){return e++<1?n(null,!0):void t.apply(this,arguments)},n,r)}function Sn(n,t){return t}function jn(n,t,r,e){var u=[];n(t,function(n,t,e){r(n,function(n,t){u=u.concat(t||[]),e(n)})},function(n){e(n,u)})}function Ln(){return cn.apply(null,Rt.call(arguments))}function On(n,t){return sn(n,1,t)}function xn(n,t){for(var r=-1,e=n.length;++rr&&(r=Gt(e+r,0)),$n(n,t,r)):-1}function Bn(n,t,r){function e(n){s.unshift(n)}function u(n){var t=qn(s,n);t>=0&&s.splice(t,1)}function i(){a--,ln(s.slice(),function(n){n()})}"function"==typeof arguments[1]&&(r=t,t=null),r=p(r||h);var c=O(n),a=c.length;if(!a)return r(null);t||(t=a);var f={},l=0,s=[];e(function(){a||r(null,f)}),ln(c,function(c){function a(){return t>l&&!v(f,c)&&xn(m,function(n){return v(f,n)})}function s(){a()&&(l++,u(s),h[h.length-1](y,f))}for(var p,h=lt(n[c])?n[c]:[n[c]],y=o(function(n,t){if(l--,t.length<=1&&(t=t[0]),n){var e={};zn(f,function(n,t){e[t]=n}),e[c]=t,r(n,e)}else f[c]=t,gt(i)}),m=h.slice(0,h.length-1),d=m.length;d--;){if(!(p=n[m[d]]))throw new Error("Has inexistant dependency");if(lt(p)&&qn(p,c)>=0)throw new Error("Has cyclic dependencies")}a()?(l++,h[h.length-1](y,f)):e(s)})}function Fn(n){return o(function(t,r){var e=o(function(r){var e=this,u=r.pop();return n(t,function(n,t,u){n.apply(e,r.concat([u]))},u)});return r.length?e.apply(this,r):e})}var Pn,Un="[object Function]",Cn="[object GeneratorFunction]",Dn=Object.prototype,Hn=Dn.toString,Nn=NaN,Qn=/^\s+|\s+$/g,Rn=/^[-+]0x[0-9a-f]+$/i,Wn=/^0b[01]+$/i,Gn=/^0o[0-7]+$/i,Jn=parseInt,Kn=1/0,Vn=1.7976931348623157e308,Xn="Expected a function",Yn=Math.max,Zn="Expected a function",_n=f("length"),nt=9007199254740991,tt=Object.prototype,rt=tt.hasOwnProperty,et=Object.getPrototypeOf,ut=Object.keys,it="[object Arguments]",ot=Object.prototype,ct=ot.hasOwnProperty,at=ot.toString,ft=ot.propertyIsEnumerable,lt=Array.isArray,st="[object String]",pt=Object.prototype,ht=pt.toString,yt=9007199254740991,mt=/^(?:0|[1-9]\d*)$/,vt=Object.prototype,dt="function"==typeof setImmediate&&setImmediate;Pn=dt?function(n){dt(n)}:"object"==typeof process&&"function"==typeof process.nextTick?process.nextTick:function(n){setTimeout(n,0)};var gt=Pn,kt=T(l),bt=M(l),wt=q(l),Et=Array.prototype.slice,St=Q($,Boolean,N),jt=Q($,R,R),Lt=T(Z),Ot=Math.ceil,xt=Math.max,At=M(Z),It=q(Z),Tt=Q(P,Boolean,N),zt=T(fn),Mt=M(fn),$t=q(fn),qt="object"==typeof process&&"function"==typeof process.nextTick?process.nextTick:gt,Bt=dn("log"),Ft=Q(P,R,R),Pt=dn("dir"),Ut=Q(I,N,Sn),Ct=Q(P,N,Sn),Dt=Q($,N,Sn),Ht=o(function(n){var t=[null].concat(n);return function(n){return n.apply(this,t)}}),Nt=T(jn),Qt=q(jn),Rt=Array.prototype.reverse,Wt=An(),Gt=Math.max,Jt=o(function(n,t){return o(function(r){return n.apply(null,t.concat(r))})}),Kt=Fn(I),Vt=Fn($),Xt={applyEach:Vt,applyEachSeries:Kt,apply:Jt,asyncify:c,auto:Bn,cargo:On,compose:Ln,concat:Qt,concatSeries:Nt,constant:Ht,detect:Dt,detectLimit:Ct,detectSeries:Ut,dir:Pt,doDuring:En,doUntil:wn,doWhilst:bn,during:kn,each:H,eachLimit:C,eachOf:$,eachOfLimit:P,eachOfSeries:I,eachSeries:D,ensureAsync:G,every:jt,everyLimit:Ft,filter:wt,filterLimit:bt,filterSeries:kt,forever:gn,iterator:J,log:Bt,map:It,mapLimit:At,mapSeries:Lt,memoize:vn,nextTick:qt,parallel:mn,parallelLimit:yn,priorityQueue:hn,queue:pn,reduce:B,reduceRight:F,reject:$t,rejectLimit:Mt,rejectSeries:zt,retry:an,seq:cn,series:on,setImmediate:gt,some:St,someLimit:Tt,sortBy:en,times:rn,timesLimit:tn,timesSeries:nn,transform:Y,unmemoize:X,until:V,waterfall:K,whilst:W,all:jt,any:St,forEach:H,forEachSeries:D,forEachLimit:C,forEachOf:$,forEachOfSeries:I,forEachOfLimit:P,inject:B,foldl:B,foldr:F,select:wt,selectLimit:bt,selectSeries:kt,wrapSync:c};n["default"]=Xt,n.applyEach=Vt,n.applyEachSeries=Kt,n.apply=Jt,n.asyncify=c,n.auto=Bn,n.cargo=On,n.compose=Ln,n.concat=Qt,n.concatSeries=Nt,n.constant=Ht,n.detect=Dt,n.detectLimit=Ct,n.detectSeries=Ut,n.dir=Pt,n.doDuring=En,n.doUntil=wn,n.doWhilst=bn,n.during=kn,n.each=H,n.eachLimit=C,n.eachOf=$,n.eachOfLimit=P,n.eachOfSeries=I,n.eachSeries=D,n.ensureAsync=G,n.every=jt,n.everyLimit=Ft,n.filter=wt,n.filterLimit=bt,n.filterSeries=kt,n.forever=gn,n.iterator=J,n.log=Bt,n.map=It,n.mapLimit=At,n.mapSeries=Lt,n.memoize=vn,n.nextTick=qt,n.parallel=mn,n.parallelLimit=yn,n.priorityQueue=hn,n.queue=pn,n.reduce=B,n.reduceRight=F,n.reject=$t,n.rejectLimit=Mt,n.rejectSeries=zt,n.retry=an,n.seq=cn,n.series=on,n.setImmediate=gt,n.some=St,n.someLimit=Tt,n.sortBy=en,n.times=rn,n.timesLimit=tn,n.timesSeries=nn,n.transform=Y,n.unmemoize=X,n.until=V,n.waterfall=K,n.whilst=W,n.all=jt,n.any=St,n.forEach=H,n.forEachSeries=D,n.forEachLimit=C,n.forEachOf=$,n.forEachOfSeries=I,n.forEachOfLimit=P,n.inject=B,n.foldl=B,n.foldr=F,n.select=wt,n.selectLimit=bt,n.selectSeries=kt,n.wrapSync=c}); +!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(n.async={})}(this,function(n){"use strict";function t(n,t,r){var e=r?r.length:0;switch(e){case 0:return n.call(t);case 1:return n.call(t,r[0]);case 2:return n.call(t,r[0],r[1]);case 3:return n.call(t,r[0],r[1],r[2])}return n.apply(t,r)}function r(n){var t=typeof n;return!!n&&("object"==t||"function"==t)}function e(n){var t=r(n)?Hn.call(n):"";return t==Un||t==Cn}function u(n){if(r(n)){var t=e(n.valueOf)?n.valueOf():n;n=r(t)?t+"":t}if("string"!=typeof n)return 0===n?n:+n;n=n.replace(Qn,"");var u=Wn.test(n);return u||Gn.test(n)?Jn(n.slice(2),u?2:8):Rn.test(n)?Nn:+n}function i(n){if(!n)return 0===n?n:0;if(n=u(n),n===Kn||n===-Kn){var t=0>n?-1:1;return t*Vn}var r=n%1;return n===n?r?n-r:n:0}function o(n,r){if("function"!=typeof n)throw new TypeError(Xn);return r=Yn(void 0===r?n.length-1:i(r),0),function(){for(var e=arguments,u=-1,i=Yn(e.length-r,0),o=Array(i);++u0&&(r=t.apply(this,arguments)),1>=n&&(t=void 0),r}}function f(n){return a(2,n)}function l(){}function s(n){return function(t){return null==t?void 0:t[n]}}function p(n){return"number"==typeof n&&n>-1&&n%1==0&&nt>=n}function h(n){return null!=n&&!("function"==typeof n&&e(n))&&p(_n(n))}function y(n,t){return rt.call(n,t)||"object"==typeof n&&t in n&&null===et(n)}function m(n){return ut(Object(n))}function v(n,t){for(var r=-1,e=Array(n);++r-1&&n%1==0&&t>n}function S(n){var t=n&&n.constructor,r="function"==typeof t&&t.prototype||vt;return n===r}function j(n){var t=S(n);if(!t&&!h(n))return m(n);var r=w(n),e=!!r,u=r||[],i=u.length;for(var o in n)!y(n,o)||e&&("length"==o||E(o,i))||t&&"constructor"==o||u.push(o);return u}function L(n){var t,r=-1;if(h(n))return t=n.length,function(){return r++,t>r?r:null};var e=j(n);return t=e.length,function(){return r++,t>r?e[r]:null}}function O(n){return function(){if(null===n)throw new Error("Callback was already called.");n.apply(this,arguments),n=null}}function x(n,t,r){function e(n){o--,n?r(n):null===u&&0>=o&&r(null)}r=f(r||l),n=n||[];for(var u,i=L(n),o=0;null!=(u=i());)o+=1,t(n[u],u,O(e));0===o&&r(null)}function A(n,t,r){function e(){var o=!0;return null===i?r(null):(t(n[i],i,O(function(n){if(n)r(n);else{if(i=u(),null===i)return r(null);o?kt(e):e()}})),void(o=!1))}r=f(r||l),n=n||[];var u=L(n),i=u();e()}function I(n){return o(function(t){var e,u=t.pop();try{e=n.apply(this,t)}catch(i){return u(i)}r(e)&&"function"==typeof e.then?e.then(function(n){u(null,n)})["catch"](function(n){u(n.message?n:new Error(n))}):u(null,e)})}function T(n,t){for(var r=-1,e=n.length;++rr&&(r=St(e+r,0)),U(n,t,r)):-1}function D(n,t,r){function e(n){h.unshift(n)}function u(n){var t=C(h,n);t>=0&&h.splice(t,1)}function i(){a--,T(h.slice(),function(n){n()})}"function"==typeof arguments[1]&&(r=t,t=null),r=f(r||l);var c=j(n),a=c.length;if(!a)return r(null);t||(t=a);var s={},p=0,h=[];e(function(){a||r(null,s)}),T(c,function(c){function a(){return t>p&&!y(s,c)&&z(v,function(n){return y(s,n)})}function f(){a()&&(p++,u(f),h[h.length-1](m,s))}for(var l,h=lt(n[c])?n[c]:[n[c]],m=o(function(n,t){if(p--,t.length<=1&&(t=t[0]),n){var e={};F(s,function(n,t){e[t]=n}),e[c]=t,r(n,e)}else s[c]=t,kt(i)}),v=h.slice(0,h.length-1),d=v.length;d--;){if(!(l=n[v[d]]))throw new Error("Has inexistant dependency");if(lt(l)&&C(l,c)>=0)throw new Error("Has cyclic dependencies")}a()?(p++,h[h.length-1](m,s)):e(f)})}function H(n,t){for(var r=-1,e=n.length,u=Array(e);++r=t;t++)kt(c.process)}}};return c}function Q(n,t){return N(n,1,t)}function R(n,t,r,e){A(n,function(n,e,u){r(t,n,function(n,r){t=r,u(n)})},function(n){e(n,t)})}function W(){var n=arguments;return o(function(t){var r=this,e=t[t.length-1];"function"==typeof e?t.pop():e=l,R(n,t,function(n,t,e){t.apply(r,n.concat([o(function(n,t){e(n,t)})]))},function(n,t){e.apply(r,[n].concat(t))})})}function G(){return W.apply(null,jt.call(arguments))}function J(n,t,r,e){var u=[];n(t,function(n,t,e){r(n,function(n,t){u=u.concat(t||[]),e(n)})},function(n){e(n,u)})}function K(n){return function(t,r,e){return n(x,t,r,e)}}function V(n){return function(t,r,e){return n(A,t,r,e)}}function X(n,t,r){return function(e,u,i,o){function c(){o&&o(r(!1,void 0))}function a(n,e,u){return o?void i(n,function(e){o&&t(e)&&(o(r(!0,n)),o=i=!1),u()}):u()}arguments.length>3?n(e,u,a,c):(o=i,i=u,n(e,a,c))}}function Y(n,t){return t}function Z(n){return function(t,r,e){e=f(e||l),t=t||[];var u=L(t);if(0>=n)return e(null);var i=!1,o=0,c=!1;!function a(){if(i&&0>=o)return e(null);for(;n>o&&!c;){var f=u();if(null===f)return i=!0,void(0>=o&&e(null));o+=1,r(t[f],f,O(function(n){o-=1,n?(e(n),c=!0):a()}))}}()}}function _(n,t,r,e){Z(t)(n,r,e)}function nn(n){return o(function(t,r){t.apply(null,r.concat([o(function(t,r){"object"==typeof console&&(t?console.error&&console.error(t):console[n]&&T(r,function(t){console[n](t)}))})]))})}function tn(n,t,r){r=r||l;var e=o(function(t,e){t?r(t):(e.push(u),n.apply(this,e))}),u=function(n,u){return n?r(n):u?void t(e):r(null)};n(u)}function rn(n,t,r){var e=0;tn(function(n){return e++<1?n(null,!0):void t.apply(this,arguments)},n,r)}function en(n,t,r){if(r=r||l,!n())return r(null);var e=o(function(u,i){return u?r(u):n.apply(this,i)?t(e):void r.apply(null,[null].concat(i))});t(e)}function un(n,t,r){var e=0;return en(function(){return++e<=1||t.apply(this,arguments)},n,r)}function on(n,t,r){return un(n,function(){return!t.apply(this,arguments)},r)}function cn(n){return function(t,r,e){return n(t,e)}}function an(n,t,r){return x(n,cn(t),r)}function fn(n,t,r,e){return Z(t)(n,cn(r),e)}function ln(n,t,r){return A(n,cn(t),r)}function sn(n){return o(function(t){var r=t.pop(),e=!0;t.push(function(){var n=arguments;e?kt(function(){r.apply(null,n)}):r.apply(null,n)}),n.apply(this,t),e=!1})}function pn(n){return!n}function hn(n,t,r,e){var u=[];n(t,function(n,t,e){r(n,function(r){r&&u.push({index:t,value:n}),e()})},function(){e(H(u.sort(function(n,t){return n.index-t.index}),s("value")))})}function yn(n){return function(t,r,e,u){return n(Z(r),t,e,u)}}function mn(n,t){function r(n){return n?e(n):void u(r)}var e=O(t||l),u=sn(n);r()}function vn(n){function t(r){function e(){return n.length&&n[r].apply(null,arguments),e.next()}return e.next=function(){return ru;u++)t[u].apply(null,n)})])))});return u.memo=r,u.unmemoized=n,u}function kn(n,t,r){r=r||l;var e=h(t)?[]:{};n(t,function(n,t,r){n(o(function(n,u){u.length<=1&&(u=u[0]),e[t]=u,r(n)}))},function(n){r(n,e)})}function bn(n,t){return kn(x,n,t)}function wn(n,t,r){return kn(Z(t),n,r)}function En(n,t){return N(function(t,r){n(t[0],r)},t,1)}function Sn(n,t){function r(n,t){return n.priority-t.priority}function e(n,t,r){for(var e=-1,u=n.length-1;u>e;){var i=e+(u-e+1>>>1);r(t,n[i])>=0?e=i:u=i-1}return e}function u(n,t,u,i){if(null!=i&&"function"!=typeof i)throw new Error("task callback must be a function");return n.started=!0,lt(t)||(t=[t]),0===t.length?kt(function(){n.drain()}):void T(t,function(t){var o={data:t,priority:u,callback:"function"==typeof i?i:l};n.tasks.splice(e(n.tasks,o,r)+1,0,o),n.tasks.length===n.concurrency&&n.saturated(),kt(n.process)})}var i=En(n,t);return i.push=function(n,t,r){u(i,n,t,r)},delete i.unshift,i}function jn(n,t,r,e){var u=Nt.call(n).reverse();R(u,t,r,e)}function Ln(n,t,r,e){hn(n,t,function(n,t){r(n,function(n){t(!n)})},e)}function On(n,t){return kn(A,n,t)}function xn(n,t,r){function e(n,t){if("number"==typeof t)n.times=parseInt(t,10)||i;else{if("object"!=typeof t)throw new Error("Unsupported argument type for 'times': "+typeof t);n.times=parseInt(t.times,10)||i,n.interval=parseInt(t.interval,10)||o}}function u(n,t){function r(n,r){return function(e){n(function(n,t){e(!n||r,{err:n,result:t})},t)}}function e(n){return function(t){setTimeout(function(){t(null)},n)}}for(;a.times;){var u=!(a.times-=1);c.push(r(a.task,u)),!u&&a.interval>0&&c.push(e(a.interval))}On(c,function(t,r){r=r[r.length-1],(n||a.callback)(r.err,r.result)})}var i=5,o=0,c=[],a={times:i,interval:o},f=arguments.length;if(1>f||f>3)throw new Error("Invalid arguments - must be either (task), (task, callback), (times, task) or (times, task, callback)");return 2>=f&&"function"==typeof n&&(r=t,t=n),"function"!=typeof n&&e(a,n),a.callback=r,a.task=t,a.callback?u():u}function An(n,t,r){function e(n,t){var r=n.criteria,e=t.criteria;return e>r?-1:r>e?1:0}Ut(n,function(n,r){t(n,function(t,e){return t?r(t):void r(null,{value:n,criteria:e})})},function(n,t){return n?r(n):void r(null,H(t.sort(e),s("value")))})}function In(n,t,r,e){for(var u=-1,i=Vt(Kt((t-n)/(r||1)),0),o=Array(i);i--;)o[e?i:++u]=n,n+=r;return o}function Tn(n,t,r){Ut(In(0,n,1),t,r)}function zn(n,t,r,e){return Ct(In(0,n,1),t,r,e)}function Mn(n,t,r){Dt(In(0,n,1),t,r)}function $n(n,t,r,e){3===arguments.length&&(e=r,r=t,t=lt(n)?[]:{}),x(n,function(n,e,u){r(t,n,e,u)},function(n){e(n,t)})}function qn(n){return function(){return(n.unmemoized||n).apply(null,arguments)}}function Bn(n,t,r){return en(function(){return!n.apply(this,arguments)},t,r)}function Fn(n,t){function r(n){return o(function(e,u){if(e)t.apply(null,[e].concat(u));else{var i=n.next();i?u.push(r(i)):u.push(t),sn(n).apply(null,u)}})}return t=f(t||l),lt(n)?n.length?void r(vn(n))():t():t(new Error("First argument to waterfall must be an array of functions"))}var Pn,Un="[object Function]",Cn="[object GeneratorFunction]",Dn=Object.prototype,Hn=Dn.toString,Nn=NaN,Qn=/^\s+|\s+$/g,Rn=/^[-+]0x[0-9a-f]+$/i,Wn=/^0b[01]+$/i,Gn=/^0o[0-7]+$/i,Jn=parseInt,Kn=1/0,Vn=1.7976931348623157e308,Xn="Expected a function",Yn=Math.max,Zn="Expected a function",_n=s("length"),nt=9007199254740991,tt=Object.prototype,rt=tt.hasOwnProperty,et=Object.getPrototypeOf,ut=Object.keys,it="[object Arguments]",ot=Object.prototype,ct=ot.hasOwnProperty,at=ot.toString,ft=ot.propertyIsEnumerable,lt=Array.isArray,st="[object String]",pt=Object.prototype,ht=pt.toString,yt=9007199254740991,mt=/^(?:0|[1-9]\d*)$/,vt=Object.prototype,dt=c(x),gt="function"==typeof setImmediate&&setImmediate;Pn=gt?function(n){gt(n)}:"object"==typeof process&&"function"==typeof process.nextTick?process.nextTick:function(n){setTimeout(n,0)};var kt=Pn,bt=c(A),wt=o(function(n,t){return o(function(r){return n.apply(null,t.concat(r))})}),Et=M(),St=Math.max,jt=Array.prototype.reverse,Lt=K(J),Ot=V(J),xt=o(function(n){var t=[null].concat(n);return function(n){return n.apply(this,t)}}),At=X(x,q,Y),It=X(_,q,Y),Tt=X(A,q,Y),zt=nn("dir"),Mt=X(x,pn,pn),$t=X(_,pn,pn),qt=K(hn),Bt=yn(hn),Ft=V(hn),Pt=nn("log"),Ut=K(dn),Ct=yn(dn),Dt=V(dn),Ht="object"==typeof process&&"function"==typeof process.nextTick?process.nextTick:kt,Nt=Array.prototype.slice,Qt=K(Ln),Rt=yn(Ln),Wt=V(Ln),Gt=X(x,Boolean,q),Jt=X(_,Boolean,q),Kt=Math.ceil,Vt=Math.max,Xt={applyEach:dt,applyEachSeries:bt,apply:wt,asyncify:I,auto:D,cargo:Q,compose:G,concat:Lt,concatSeries:Ot,constant:xt,detect:At,detectLimit:It,detectSeries:Tt,dir:zt,doDuring:rn,doUntil:on,doWhilst:un,during:tn,each:an,eachLimit:fn,eachOf:x,eachOfLimit:_,eachOfSeries:A,eachSeries:ln,ensureAsync:sn,every:Mt,everyLimit:$t,filter:qt,filterLimit:Bt,filterSeries:Ft,forever:mn,iterator:vn,log:Pt,map:Ut,mapLimit:Ct,mapSeries:Dt,memoize:gn,nextTick:Ht,parallel:bn,parallelLimit:wn,priorityQueue:Sn,queue:En,reduce:R,reduceRight:jn,reject:Qt,rejectLimit:Rt,rejectSeries:Wt,retry:xn,seq:W,series:On,setImmediate:kt,some:Gt,someLimit:Jt,sortBy:An,times:Tn,timesLimit:zn,timesSeries:Mn,transform:$n,unmemoize:qn,until:Bn,waterfall:Fn,whilst:en,all:Mt,any:Gt,forEach:an,forEachSeries:ln,forEachLimit:fn,forEachOf:x,forEachOfSeries:A,forEachOfLimit:_,inject:R,foldl:R,foldr:jn,select:qt,selectLimit:Bt,selectSeries:Ft,wrapSync:I};n["default"]=Xt,n.applyEach=dt,n.applyEachSeries=bt,n.apply=wt,n.asyncify=I,n.auto=D,n.cargo=Q,n.compose=G,n.concat=Lt,n.concatSeries=Ot,n.constant=xt,n.detect=At,n.detectLimit=It,n.detectSeries=Tt,n.dir=zt,n.doDuring=rn,n.doUntil=on,n.doWhilst=un,n.during=tn,n.each=an,n.eachLimit=fn,n.eachOf=x,n.eachOfLimit=_,n.eachOfSeries=A,n.eachSeries=ln,n.ensureAsync=sn,n.every=Mt,n.everyLimit=$t,n.filter=qt,n.filterLimit=Bt,n.filterSeries=Ft,n.forever=mn,n.iterator=vn,n.log=Pt,n.map=Ut,n.mapLimit=Ct,n.mapSeries=Dt,n.memoize=gn,n.nextTick=Ht,n.parallel=bn,n.parallelLimit=wn,n.priorityQueue=Sn,n.queue=En,n.reduce=R,n.reduceRight=jn,n.reject=Qt,n.rejectLimit=Rt,n.rejectSeries=Wt,n.retry=xn,n.seq=W,n.series=On,n.setImmediate=kt,n.some=Gt,n.someLimit=Jt,n.sortBy=An,n.times=Tn,n.timesLimit=zn,n.timesSeries=Mn,n.transform=$n,n.unmemoize=qn,n.until=Bn,n.waterfall=Fn,n.whilst=en,n.all=Mt,n.any=Gt,n.forEach=an,n.forEachSeries=ln,n.forEachLimit=fn,n.forEachOf=x,n.forEachOfSeries=A,n.forEachOfLimit=_,n.inject=R,n.foldl=R,n.foldr=jn,n.select=qt,n.selectLimit=Bt,n.selectSeries=Ft,n.wrapSync=I}); //# sourceMappingURL=dist/async.min.map \ No newline at end of file diff --git a/build/bower.json b/build/bower.json new file mode 100644 index 000000000..b2a4f3c71 --- /dev/null +++ b/build/bower.json @@ -0,0 +1,68 @@ +{ + "name": "async", + "description": "Higher-order functions and common patterns for asynchronous code", + "main": "index.js", + "keywords": [ + "async", + "callback", + "module", + "utility" + ], + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/caolan/async.git" + }, + "devDependencies": { + "babel-cli": "^6.3.17", + "babel-core": "^6.3.26", + "babel-plugin-add-module-exports": "~0.1.2", + "babel-plugin-transform-es2015-modules-commonjs": "^6.3.16", + "babel-preset-es2015": "^6.3.13", + "babelify": "^7.2.0", + "benchmark": "bestiejs/benchmark.js", + "bluebird": "^2.9.32", + "chai": "^3.1.0", + "coveralls": "^2.11.2", + "es6-promise": "^2.3.0", + "fs-extra": "^0.26.3", + "gulp": "~3.9.0", + "jscs": "^1.13.1", + "jshint": "~2.8.0", + "karma": "^0.13.2", + "karma-browserify": "^4.2.1", + "karma-firefox-launcher": "^0.1.6", + "karma-mocha": "^0.2.0", + "karma-mocha-reporter": "^1.0.2", + "mocha": "^2.2.5", + "native-promise-only": "^0.8.0-a", + "nodeunit": ">0.0.0", + "nyc": "^2.1.0", + "recursive-readdir": "^1.3.0", + "rimraf": "^2.5.0", + "rollup": "^0.25.0", + "rollup-plugin-npm": "~1.3.0", + "rsvp": "^3.0.18", + "semver": "^4.3.6", + "uglify-js": "~2.4.0", + "vinyl-buffer": "~1.0.0", + "vinyl-source-stream": "~1.1.0", + "xyz": "^0.5.0", + "yargs": "~3.9.1" + }, + "moduleType": [ + "amd", + "globals", + "node" + ], + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ], + "authors": [ + "Caolan McMahon" + ] +} \ No newline at end of file diff --git a/build/component.json b/build/component.json new file mode 100644 index 000000000..b699237fc --- /dev/null +++ b/build/component.json @@ -0,0 +1,17 @@ +{ + "name": "async", + "description": "Higher-order functions and common patterns for asynchronous code", + "version": "2.0.0-alpha", + "keywords": [ + "async", + "callback", + "module", + "utility" + ], + "license": "MIT", + "main": "index.js", + "repository": "caolan/async", + "scripts": [ + "index.js" + ] +} \ No newline at end of file diff --git a/build/es/apply.js b/build/es/apply.js new file mode 100644 index 000000000..eeceb43a7 --- /dev/null +++ b/build/es/apply.js @@ -0,0 +1,9 @@ +'use strict'; + +import rest from 'lodash-es/rest'; + +export default rest(function(fn, args) { + return rest(function(callArgs) { + return fn.apply(null, args.concat(callArgs)); + }); +}); diff --git a/build/es/applyEach.js b/build/es/applyEach.js new file mode 100644 index 000000000..2c5f001da --- /dev/null +++ b/build/es/applyEach.js @@ -0,0 +1,6 @@ +'use strict'; + +import applyEach from './internal/applyEach'; +import eachOf from './eachOf'; + +export default applyEach(eachOf); diff --git a/build/es/applyEachSeries.js b/build/es/applyEachSeries.js new file mode 100644 index 000000000..e917618d6 --- /dev/null +++ b/build/es/applyEachSeries.js @@ -0,0 +1,6 @@ +'use strict'; + +import applyEach from './internal/applyEach'; +import eachOfSeries from './eachOfSeries'; + +export default applyEach(eachOfSeries); diff --git a/build/es/asyncify.js b/build/es/asyncify.js new file mode 100644 index 000000000..9239e9904 --- /dev/null +++ b/build/es/asyncify.js @@ -0,0 +1,26 @@ +'use strict'; + +import isObject from 'lodash-es/isObject'; +import rest from 'lodash-es/rest'; + +export default function asyncify(func) { + return rest(function (args) { + var callback = args.pop(); + var result; + try { + result = func.apply(this, args); + } catch (e) { + return callback(e); + } + // if result is Promise object + if (isObject(result) && typeof result.then === 'function') { + result.then(function(value) { + callback(null, value); + })['catch'](function(err) { + callback(err.message ? err : new Error(err)); + }); + } else { + callback(null, result); + } + }); +} diff --git a/build/es/auto.js b/build/es/auto.js new file mode 100644 index 000000000..19f4215f2 --- /dev/null +++ b/build/es/auto.js @@ -0,0 +1,109 @@ +'use strict'; + +import arrayEach from 'lodash-es/internal/arrayEach'; +import arrayEvery from 'lodash-es/internal/arrayEvery'; +import baseHas from 'lodash-es/internal/baseHas'; +import forOwn from 'lodash-es/forOwn'; +import indexOf from 'lodash-es/indexOf'; +import isArray from 'lodash-es/isArray'; +import okeys from 'lodash-es/keys'; +import noop from 'lodash-es/noop'; +import once from 'lodash-es/once'; +import rest from 'lodash-es/rest'; + +import setImmediate from './internal/setImmediate'; + +export default function (tasks, concurrency, callback) { + if (typeof arguments[1] === 'function') { + // concurrency is optional, shift the args. + callback = concurrency; + concurrency = null; + } + callback = once(callback || noop); + var keys = okeys(tasks); + var remainingTasks = keys.length; + if (!remainingTasks) { + return callback(null); + } + if (!concurrency) { + concurrency = remainingTasks; + } + + var results = {}; + var runningTasks = 0; + + var listeners = []; + function addListener(fn) { + listeners.unshift(fn); + } + function removeListener(fn) { + var idx = indexOf(listeners, fn); + if (idx >= 0) listeners.splice(idx, 1); + } + function taskComplete() { + remainingTasks--; + arrayEach(listeners.slice(), function (fn) { + fn(); + }); + } + + addListener(function () { + if (!remainingTasks) { + callback(null, results); + } + }); + + arrayEach(keys, function (k) { + var task = isArray(tasks[k]) ? tasks[k]: [tasks[k]]; + var taskCallback = rest(function(err, args) { + runningTasks--; + if (args.length <= 1) { + args = args[0]; + } + if (err) { + var safeResults = {}; + forOwn(results, function(val, rkey) { + safeResults[rkey] = val; + }); + safeResults[k] = args; + callback(err, safeResults); + } + else { + results[k] = args; + setImmediate(taskComplete); + } + }); + var requires = task.slice(0, task.length - 1); + // prevent dead-locks + var len = requires.length; + var dep; + while (len--) { + if (!(dep = tasks[requires[len]])) { + throw new Error('Has inexistant dependency'); + } + if (isArray(dep) && indexOf(dep, k) >= 0) { + throw new Error('Has cyclic dependencies'); + } + } + function ready() { + return runningTasks < concurrency && !baseHas(results, k) && + arrayEvery(requires, function (x) { + return baseHas(results, x); + }); + } + if (ready()) { + runningTasks++; + task[task.length - 1](taskCallback, results); + } + else { + addListener(listener); + } + function listener() { + if (ready()) { + runningTasks++; + removeListener(listener); + task[task.length - 1](taskCallback, results); + } + } + }); +} diff --git a/build/es/cargo.js b/build/es/cargo.js new file mode 100644 index 000000000..b90ab0f8a --- /dev/null +++ b/build/es/cargo.js @@ -0,0 +1,7 @@ +'use strict'; + +import queue from './internal/queue'; + +export default function cargo(worker, payload) { + return queue(worker, 1, payload); +} diff --git a/build/es/compose.js b/build/es/compose.js new file mode 100644 index 000000000..9e925211f --- /dev/null +++ b/build/es/compose.js @@ -0,0 +1,9 @@ +'use strict'; + +import seq from './seq'; + +var reverse = Array.prototype.reverse; + +export default function compose(/* functions... */) { + return seq.apply(null, reverse.call(arguments)); +} diff --git a/build/es/concat.js b/build/es/concat.js new file mode 100644 index 000000000..af019a370 --- /dev/null +++ b/build/es/concat.js @@ -0,0 +1,6 @@ +'use strict'; + +import concat from './internal/concat'; +import doParallel from './internal/doParallel'; + +export default doParallel(concat); diff --git a/build/es/concatSeries.js b/build/es/concatSeries.js new file mode 100644 index 000000000..76f404327 --- /dev/null +++ b/build/es/concatSeries.js @@ -0,0 +1,6 @@ +'use strict'; + +import concat from './internal/concat'; +import doSeries from './internal/doSeries'; + +export default doSeries(concat); diff --git a/build/es/constant.js b/build/es/constant.js new file mode 100644 index 000000000..492d11bbd --- /dev/null +++ b/build/es/constant.js @@ -0,0 +1,10 @@ +'use strict'; + +import rest from 'lodash-es/rest'; + +export default rest(function(values) { + var args = [null].concat(values); + return function (cb) { + return cb.apply(this, args); + }; +}); diff --git a/build/es/detect.js b/build/es/detect.js new file mode 100644 index 000000000..19fffef6a --- /dev/null +++ b/build/es/detect.js @@ -0,0 +1,9 @@ +'use strict'; + +import identity from 'lodash-es/identity'; + +import createTester from './internal/createTester'; +import eachOf from './eachOf'; +import findGetResult from './internal/findGetResult'; + +export default createTester(eachOf, identity, findGetResult); diff --git a/build/es/detectLimit.js b/build/es/detectLimit.js new file mode 100644 index 000000000..0fc46db12 --- /dev/null +++ b/build/es/detectLimit.js @@ -0,0 +1,9 @@ +'use strict'; + +import identity from 'lodash-es/identity'; + +import createTester from './internal/createTester'; +import eachOfLimit from './eachOfLimit'; +import findGetResult from './internal/findGetResult'; + +export default createTester(eachOfLimit, identity, findGetResult); diff --git a/build/es/detectSeries.js b/build/es/detectSeries.js new file mode 100644 index 000000000..26f48bac0 --- /dev/null +++ b/build/es/detectSeries.js @@ -0,0 +1,9 @@ +'use strict'; + +import identity from 'lodash-es/identity'; + +import createTester from './internal/createTester'; +import eachOfSeries from './eachOfSeries'; +import findGetResult from './internal/findGetResult'; + +export default createTester(eachOfSeries, identity, findGetResult); diff --git a/build/es/dir.js b/build/es/dir.js new file mode 100644 index 000000000..049b41667 --- /dev/null +++ b/build/es/dir.js @@ -0,0 +1,5 @@ +'use strict'; + +import consoleFunc from './internal/consoleFunc'; + +export default consoleFunc('dir'); diff --git a/build/es/doDuring.js b/build/es/doDuring.js new file mode 100644 index 000000000..c13e62b8b --- /dev/null +++ b/build/es/doDuring.js @@ -0,0 +1,12 @@ +'use strict'; + +import during from './during'; + +export default function doDuring(iterator, test, cb) { + var calls = 0; + + during(function(next) { + if (calls++ < 1) return next(null, true); + test.apply(this, arguments); + }, iterator, cb); +} diff --git a/build/es/doUntil.js b/build/es/doUntil.js new file mode 100644 index 000000000..3bc783fb7 --- /dev/null +++ b/build/es/doUntil.js @@ -0,0 +1,9 @@ +'use strict'; + +import doWhilst from './doWhilst'; + +export default function doUntil(iterator, test, cb) { + return doWhilst(iterator, function() { + return !test.apply(this, arguments); + }, cb); +} diff --git a/build/es/doWhilst.js b/build/es/doWhilst.js new file mode 100644 index 000000000..b05d1d829 --- /dev/null +++ b/build/es/doWhilst.js @@ -0,0 +1,10 @@ +'use strict'; + +import whilst from './whilst'; + +export default function doWhilst(iterator, test, cb) { + var calls = 0; + return whilst(function() { + return ++calls <= 1 || test.apply(this, arguments); + }, iterator, cb); +} diff --git a/build/es/during.js b/build/es/during.js new file mode 100644 index 000000000..e9809cdc1 --- /dev/null +++ b/build/es/during.js @@ -0,0 +1,25 @@ +'use strict'; + +import noop from 'lodash-es/noop'; +import rest from 'lodash-es/rest'; + +export default function during(test, iterator, cb) { + cb = cb || noop; + + var next = rest(function(err, args) { + if (err) { + cb(err); + } else { + args.push(check); + test.apply(this, args); + } + }); + + var check = function(err, truth) { + if (err) return cb(err); + if (!truth) return cb(null); + iterator(next); + }; + + test(check); +} diff --git a/build/es/each.js b/build/es/each.js new file mode 100644 index 000000000..384bb0a89 --- /dev/null +++ b/build/es/each.js @@ -0,0 +1,8 @@ +'use strict'; + +import eachOf from './eachOf'; +import withoutIndex from './internal/withoutIndex'; + +export default function each(arr, iterator, cb) { + return eachOf(arr, withoutIndex(iterator), cb); +} diff --git a/build/es/eachLimit.js b/build/es/eachLimit.js new file mode 100644 index 000000000..5009cc355 --- /dev/null +++ b/build/es/eachLimit.js @@ -0,0 +1,9 @@ +'use strict'; + +import eachOfLimit from './internal/eachOfLimit'; +import withoutIndex from './internal/withoutIndex'; + + +export default function eachLimit(arr, limit, iterator, cb) { + return eachOfLimit(limit)(arr, withoutIndex(iterator), cb); +} diff --git a/build/es/eachOf.js b/build/es/eachOf.js new file mode 100644 index 000000000..e06f2bbd0 --- /dev/null +++ b/build/es/eachOf.js @@ -0,0 +1,34 @@ +'use strict'; + +import once from 'lodash-es/once'; +import noop from 'lodash-es/noop'; + +import keyIterator from './internal/keyIterator'; +import onlyOnce from './internal/onlyOnce'; + +export default function eachOf(object, iterator, callback) { + callback = once(callback || noop); + object = object || []; + + var iter = keyIterator(object); + var key, completed = 0; + + while ((key = iter()) != null) { + completed += 1; + iterator(object[key], key, onlyOnce(done)); + } + + if (completed === 0) callback(null); + + function done(err) { + completed--; + if (err) { + callback(err); + } + // Check key is null in case iterator isn't exhausted + // and done resolved synchronously. + else if (key === null && completed <= 0) { + callback(null); + } + } +} diff --git a/build/es/eachOfLimit.js b/build/es/eachOfLimit.js new file mode 100644 index 000000000..e38147412 --- /dev/null +++ b/build/es/eachOfLimit.js @@ -0,0 +1,7 @@ +'use strict'; + +import _eachOfLimit from './internal/eachOfLimit'; + +export default function eachOfLimit(obj, limit, iterator, cb) { + _eachOfLimit(limit)(obj, iterator, cb); +} diff --git a/build/es/eachOfSeries.js b/build/es/eachOfSeries.js new file mode 100644 index 000000000..2456bbb9f --- /dev/null +++ b/build/es/eachOfSeries.js @@ -0,0 +1,40 @@ +'use strict'; + +import once from 'lodash-es/once'; +import noop from 'lodash-es/noop'; + +import keyIterator from './internal/keyIterator'; +import onlyOnce from './internal/onlyOnce'; +import setImmediate from './setImmediate'; + +export default function eachOfSeries(obj, iterator, callback) { + callback = once(callback || noop); + obj = obj || []; + var nextKey = keyIterator(obj); + var key = nextKey(); + + function iterate() { + var sync = true; + if (key === null) { + return callback(null); + } + iterator(obj[key], key, onlyOnce(function(err) { + if (err) { + callback(err); + } else { + key = nextKey(); + if (key === null) { + return callback(null); + } else { + if (sync) { + setImmediate(iterate); + } else { + iterate(); + } + } + } + })); + sync = false; + } + iterate(); +} diff --git a/build/es/eachSeries.js b/build/es/eachSeries.js new file mode 100644 index 000000000..fd08c0407 --- /dev/null +++ b/build/es/eachSeries.js @@ -0,0 +1,8 @@ +'use strict'; + +import eachOfSeries from './eachOfSeries'; +import withoutIndex from './internal/withoutIndex'; + +export default function eachSeries(arr, iterator, cb) { + return eachOfSeries(arr, withoutIndex(iterator), cb); +} diff --git a/build/es/ensureAsync.js b/build/es/ensureAsync.js new file mode 100644 index 000000000..ba9affd57 --- /dev/null +++ b/build/es/ensureAsync.js @@ -0,0 +1,24 @@ +'use strict'; + +import rest from 'lodash-es/rest'; + +import setImmediate from './internal/setImmediate'; + +export default function ensureAsync(fn) { + return rest(function (args) { + var callback = args.pop(); + var sync = true; + args.push(function () { + var innerArgs = arguments; + if (sync) { + setImmediate(function () { + callback.apply(null, innerArgs); + }); + } else { + callback.apply(null, innerArgs); + } + }); + fn.apply(this, args); + sync = false; + }); +} diff --git a/build/es/every.js b/build/es/every.js new file mode 100644 index 000000000..7ee371376 --- /dev/null +++ b/build/es/every.js @@ -0,0 +1,7 @@ +'use strict'; + +import createTester from './internal/createTester'; +import eachOf from './eachOf'; +import notId from './internal/notId'; + +export default createTester(eachOf, notId, notId); diff --git a/build/es/everyLimit.js b/build/es/everyLimit.js new file mode 100644 index 000000000..4b3fc5fb4 --- /dev/null +++ b/build/es/everyLimit.js @@ -0,0 +1,7 @@ +'use strict'; + +import createTester from './internal/createTester'; +import eachOfLimit from './eachOfLimit'; +import notId from './internal/notId'; + +export default createTester(eachOfLimit, notId, notId); diff --git a/build/es/filter.js b/build/es/filter.js new file mode 100644 index 000000000..36af2b1c8 --- /dev/null +++ b/build/es/filter.js @@ -0,0 +1,6 @@ +'use strict'; + +import filter from './internal/filter'; +import doParallel from './internal/doParallel'; + +export default doParallel(filter); diff --git a/build/es/filterLimit.js b/build/es/filterLimit.js new file mode 100644 index 000000000..8fe237ffc --- /dev/null +++ b/build/es/filterLimit.js @@ -0,0 +1,6 @@ +'use strict'; + +import filter from './internal/filter'; +import doParallelLimit from './internal/doParallelLimit'; + +export default doParallelLimit(filter); diff --git a/build/es/filterSeries.js b/build/es/filterSeries.js new file mode 100644 index 000000000..861ca8b20 --- /dev/null +++ b/build/es/filterSeries.js @@ -0,0 +1,6 @@ +'use strict'; + +import filter from './internal/filter'; +import doSeries from './internal/doSeries'; + +export default doSeries(filter); diff --git a/build/es/forever.js b/build/es/forever.js new file mode 100644 index 000000000..fca06846e --- /dev/null +++ b/build/es/forever.js @@ -0,0 +1,17 @@ +'use strict'; + +import noop from 'lodash-es/noop'; + +import onlyOnce from './internal/onlyOnce'; +import ensureAsync from './ensureAsync'; + +export default function forever(fn, cb) { + var done = onlyOnce(cb || noop); + var task = ensureAsync(fn); + + function next(err) { + if (err) return done(err); + task(next); + } + next(); +} diff --git a/build/es/index.js b/build/es/index.js new file mode 100644 index 000000000..a165909d4 --- /dev/null +++ b/build/es/index.js @@ -0,0 +1,228 @@ +'use strict'; + +import applyEach from './applyEach'; +import applyEachSeries from './applyEachSeries'; +import apply from './apply'; +import asyncify from './asyncify'; +import auto from './auto'; +import cargo from './cargo'; +import compose from './compose'; +import concat from './concat'; +import concatSeries from './concatSeries'; +import constant from './constant'; +import detect from './detect'; +import detectLimit from './detectLimit'; +import detectSeries from './detectSeries'; +import dir from './dir'; +import doDuring from './doDuring'; +import doUntil from './doUntil'; +import doWhilst from './doWhilst'; +import during from './during'; +import each from './each'; +import eachLimit from './eachLimit'; +import eachOf from './eachOf'; +import eachOfLimit from './eachOfLimit'; +import eachOfSeries from './eachOfSeries'; +import eachSeries from './eachSeries'; +import ensureAsync from './ensureAsync'; +import every from './every'; +import everyLimit from './everyLimit'; +import filter from './filter'; +import filterLimit from './filterLimit'; +import filterSeries from './filterSeries'; +import forever from './forever'; +import iterator from './iterator'; +import log from './log'; +import map from './map'; +import mapLimit from './mapLimit'; +import mapSeries from './mapSeries'; +import memoize from './memoize'; +import nextTick from './nextTick'; +import parallel from './parallel'; +import parallelLimit from './parallelLimit'; +import priorityQueue from './priorityQueue'; +import queue from './queue'; +import reduce from './reduce'; +import reduceRight from './reduceRight'; +import reject from './reject'; +import rejectLimit from './rejectLimit'; +import rejectSeries from './rejectSeries'; +import retry from './retry'; +import seq from './seq'; +import series from './series'; +import setImmediate from './setImmediate'; +import some from './some'; +import someLimit from './someLimit'; +import sortBy from './sortBy'; +import times from './times'; +import timesLimit from './timesLimit'; +import timesSeries from './timesSeries'; +import transform from './transform'; +import unmemoize from './unmemoize'; +import until from './until'; +import waterfall from './waterfall'; +import whilst from './whilst'; + +export default { + applyEach: applyEach, + applyEachSeries: applyEachSeries, + apply: apply, + asyncify: asyncify, + auto: auto, + cargo: cargo, + compose: compose, + concat: concat, + concatSeries: concatSeries, + constant: constant, + detect: detect, + detectLimit: detectLimit, + detectSeries: detectSeries, + dir: dir, + doDuring: doDuring, + doUntil: doUntil, + doWhilst: doWhilst, + during: during, + each: each, + eachLimit: eachLimit, + eachOf: eachOf, + eachOfLimit: eachOfLimit, + eachOfSeries: eachOfSeries, + eachSeries: eachSeries, + ensureAsync: ensureAsync, + every: every, + everyLimit: everyLimit, + filter: filter, + filterLimit: filterLimit, + filterSeries: filterSeries, + forever: forever, + iterator: iterator, + log: log, + map: map, + mapLimit: mapLimit, + mapSeries: mapSeries, + memoize: memoize, + nextTick: nextTick, + parallel: parallel, + parallelLimit: parallelLimit, + priorityQueue: priorityQueue, + queue: queue, + reduce: reduce, + reduceRight: reduceRight, + reject: reject, + rejectLimit: rejectLimit, + rejectSeries: rejectSeries, + retry: retry, + seq: seq, + series: series, + setImmediate: setImmediate, + some: some, + someLimit: someLimit, + sortBy: sortBy, + times: times, + timesLimit: timesLimit, + timesSeries: timesSeries, + transform: transform, + unmemoize: unmemoize, + until: until, + waterfall: waterfall, + whilst: whilst, + + // aliases + all: every, + any: some, + forEach: each, + forEachSeries: eachSeries, + forEachLimit: eachLimit, + forEachOf: eachOf, + forEachOfSeries: eachOfSeries, + forEachOfLimit: eachOfLimit, + inject: reduce, + foldl: reduce, + foldr: reduceRight, + select: filter, + selectLimit: filterLimit, + selectSeries: filterSeries, + wrapSync: asyncify +}; + +export { + applyEach as applyEach, + applyEachSeries as applyEachSeries, + apply as apply, + asyncify as asyncify, + auto as auto, + cargo as cargo, + compose as compose, + concat as concat, + concatSeries as concatSeries, + constant as constant, + detect as detect, + detectLimit as detectLimit, + detectSeries as detectSeries, + dir as dir, + doDuring as doDuring, + doUntil as doUntil, + doWhilst as doWhilst, + during as during, + each as each, + eachLimit as eachLimit, + eachOf as eachOf, + eachOfLimit as eachOfLimit, + eachOfSeries as eachOfSeries, + eachSeries as eachSeries, + ensureAsync as ensureAsync, + every as every, + everyLimit as everyLimit, + filter as filter, + filterLimit as filterLimit, + filterSeries as filterSeries, + forever as forever, + iterator as iterator, + log as log, + map as map, + mapLimit as mapLimit, + mapSeries as mapSeries, + memoize as memoize, + nextTick as nextTick, + parallel as parallel, + parallelLimit as parallelLimit, + priorityQueue as priorityQueue, + queue as queue, + reduce as reduce, + reduceRight as reduceRight, + reject as reject, + rejectLimit as rejectLimit, + rejectSeries as rejectSeries, + retry as retry, + seq as seq, + series as series, + setImmediate as setImmediate, + some as some, + someLimit as someLimit, + sortBy as sortBy, + times as times, + timesLimit as timesLimit, + timesSeries as timesSeries, + transform as transform, + unmemoize as unmemoize, + until as until, + waterfall as waterfall, + whilst as whilst, + + // Aliases + every as all, + some as any, + each as forEach, + eachSeries as forEachSeries, + eachLimit as forEachLimit, + eachOf as forEachOf, + eachOfSeries as forEachOfSeries, + eachOfLimit as forEachOfLimit, + reduce as inject, + reduce as foldl, + reduceRight as foldr, + filter as select, + filterLimit as selectLimit, + filterSeries as selectSeries, + asyncify as wrapSync +}; diff --git a/build/es/internal/applyEach.js b/build/es/internal/applyEach.js new file mode 100644 index 000000000..1467363b7 --- /dev/null +++ b/build/es/internal/applyEach.js @@ -0,0 +1,22 @@ +'use strict'; + +import rest from 'lodash-es/rest'; + +export default function applyEach(eachfn) { + return rest(function(fns, args) { + var go = rest(function(args) { + var that = this; + var callback = args.pop(); + return eachfn(fns, function (fn, _, cb) { + fn.apply(that, args.concat([cb])); + }, + callback); + }); + if (args.length) { + return go.apply(this, args); + } + else { + return go; + } + }); +} diff --git a/build/es/internal/concat.js b/build/es/internal/concat.js new file mode 100644 index 000000000..482ad3a1c --- /dev/null +++ b/build/es/internal/concat.js @@ -0,0 +1,13 @@ +'use strict'; + +export default function concat(eachfn, arr, fn, callback) { + var result = []; + eachfn(arr, function (x, index, cb) { + fn(x, function (err, y) { + result = result.concat(y || []); + cb(err); + }); + }, function (err) { + callback(err, result); + }); +} diff --git a/build/es/internal/consoleFunc.js b/build/es/internal/consoleFunc.js new file mode 100644 index 000000000..7796d876e --- /dev/null +++ b/build/es/internal/consoleFunc.js @@ -0,0 +1,23 @@ +'use strict'; + +import arrayEach from 'lodash-es/internal/arrayEach'; +import rest from 'lodash-es/rest'; + +export default function consoleFunc(name) { + return rest(function (fn, args) { + fn.apply(null, args.concat([rest(function (err, args) { + if (typeof console === 'object') { + if (err) { + if (console.error) { + console.error(err); + } + } + else if (console[name]) { + arrayEach(args, function (x) { + console[name](x); + }); + } + } + })])); + }); +} diff --git a/build/es/internal/createTester.js b/build/es/internal/createTester.js new file mode 100644 index 000000000..1bf2e8904 --- /dev/null +++ b/build/es/internal/createTester.js @@ -0,0 +1,26 @@ +'use strict'; + +export default function _createTester(eachfn, check, getResult) { + return function(arr, limit, iterator, cb) { + function done() { + if (cb) cb(getResult(false, void 0)); + } + function iteratee(x, _, callback) { + if (!cb) return callback(); + iterator(x, function (v) { + if (cb && check(v)) { + cb(getResult(true, x)); + cb = iterator = false; + } + callback(); + }); + } + if (arguments.length > 3) { + eachfn(arr, limit, iteratee, done); + } else { + cb = iterator; + iterator = limit; + eachfn(arr, iteratee, done); + } + }; +} diff --git a/build/es/internal/doParallel.js b/build/es/internal/doParallel.js new file mode 100644 index 000000000..19c90fc06 --- /dev/null +++ b/build/es/internal/doParallel.js @@ -0,0 +1,9 @@ +'use strict'; + +import eachOf from '../eachOf'; + +export default function doParallel(fn) { + return function (obj, iterator, callback) { + return fn(eachOf, obj, iterator, callback); + }; +} diff --git a/build/es/internal/doParallelLimit.js b/build/es/internal/doParallelLimit.js new file mode 100644 index 000000000..0c3bcf7b7 --- /dev/null +++ b/build/es/internal/doParallelLimit.js @@ -0,0 +1,9 @@ +'use strict'; + +import eachOfLimit from './eachOfLimit'; + +export default function doParallelLimit(fn) { + return function (obj, limit, iterator, callback) { + return fn(eachOfLimit(limit), obj, iterator, callback); + }; +} diff --git a/build/es/internal/doSeries.js b/build/es/internal/doSeries.js new file mode 100644 index 000000000..c7f1d99b0 --- /dev/null +++ b/build/es/internal/doSeries.js @@ -0,0 +1,9 @@ +'use strict'; + +import eachOfSeries from '../eachOfSeries'; + +export default function doSeries(fn) { + return function (obj, iterator, callback) { + return fn(eachOfSeries, obj, iterator, callback); + }; +} diff --git a/build/es/internal/eachOfLimit.js b/build/es/internal/eachOfLimit.js new file mode 100644 index 000000000..5235278cf --- /dev/null +++ b/build/es/internal/eachOfLimit.js @@ -0,0 +1,49 @@ +'use strict'; + +import noop from 'lodash-es/noop'; +import once from 'lodash-es/once'; + +import keyIterator from './keyIterator'; +import onlyOnce from './onlyOnce'; + +export default function _eachOfLimit(limit) { + return function (obj, iterator, callback) { + callback = once(callback || noop); + obj = obj || []; + var nextKey = keyIterator(obj); + if (limit <= 0) { + return callback(null); + } + var done = false; + var running = 0; + var errored = false; + + (function replenish () { + if (done && running <= 0) { + return callback(null); + } + + while (running < limit && !errored) { + var key = nextKey(); + if (key === null) { + done = true; + if (running <= 0) { + callback(null); + } + return; + } + running += 1; + iterator(obj[key], key, onlyOnce(function (err) { + running -= 1; + if (err) { + callback(err); + errored = true; + } + else { + replenish(); + } + })); + } + })(); + }; +} diff --git a/build/es/internal/filter.js b/build/es/internal/filter.js new file mode 100644 index 000000000..f58eb5226 --- /dev/null +++ b/build/es/internal/filter.js @@ -0,0 +1,20 @@ +'use strict'; + +import arrayMap from 'lodash-es/internal/arrayMap'; +import property from 'lodash-es/internal/baseProperty'; + +export default function _filter(eachfn, arr, iterator, callback) { + var results = []; + eachfn(arr, function (x, index, callback) { + iterator(x, function (v) { + if (v) { + results.push({index: index, value: x}); + } + callback(); + }); + }, function () { + callback(arrayMap(results.sort(function (a, b) { + return a.index - b.index; + }), property('value'))); + }); +} diff --git a/build/es/internal/findGetResult.js b/build/es/internal/findGetResult.js new file mode 100644 index 000000000..3aeb07dfd --- /dev/null +++ b/build/es/internal/findGetResult.js @@ -0,0 +1,5 @@ +'use strict'; + +export default function _findGetResult(v, x) { + return x; +} diff --git a/build/es/internal/keyIterator.js b/build/es/internal/keyIterator.js new file mode 100644 index 000000000..9f12beb33 --- /dev/null +++ b/build/es/internal/keyIterator.js @@ -0,0 +1,23 @@ +'use strict'; + +import isArrayLike from 'lodash-es/isArrayLike'; +import keys from 'lodash-es/keys'; + +export default function keyIterator(coll) { + var i = -1; + var len; + if (isArrayLike(coll)) { + len = coll.length; + return function next() { + i++; + return i < len ? i : null; + }; + } else { + var okeys = keys(coll); + len = okeys.length; + return function next() { + i++; + return i < len ? okeys[i] : null; + }; + } +} diff --git a/build/es/internal/map.js b/build/es/internal/map.js new file mode 100644 index 000000000..5057f4907 --- /dev/null +++ b/build/es/internal/map.js @@ -0,0 +1,19 @@ +'use strict'; + +import isArrayLike from 'lodash-es/isArrayLike'; +import noop from 'lodash-es/noop'; +import once from 'lodash-es/once'; + +export default function _asyncMap(eachfn, arr, iterator, callback) { + callback = once(callback || noop); + arr = arr || []; + var results = isArrayLike(arr) ? [] : {}; + eachfn(arr, function (value, index, callback) { + iterator(value, function (err, v) { + results[index] = v; + callback(err); + }); + }, function (err) { + callback(err, results); + }); +} diff --git a/build/es/internal/notId.js b/build/es/internal/notId.js new file mode 100644 index 000000000..d936aabba --- /dev/null +++ b/build/es/internal/notId.js @@ -0,0 +1,5 @@ +'use strict'; + +export default function notId(v) { + return !v; +} diff --git a/build/es/internal/onlyOnce.js b/build/es/internal/onlyOnce.js new file mode 100644 index 000000000..f4241c813 --- /dev/null +++ b/build/es/internal/onlyOnce.js @@ -0,0 +1,9 @@ +'use strict'; + +export default function onlyOnce(fn) { + return function() { + if (fn === null) throw new Error("Callback was already called."); + fn.apply(this, arguments); + fn = null; + }; +} diff --git a/build/es/internal/parallel.js b/build/es/internal/parallel.js new file mode 100644 index 000000000..b53290823 --- /dev/null +++ b/build/es/internal/parallel.js @@ -0,0 +1,22 @@ +'use strict'; + +import noop from 'lodash-es/noop'; +import isArrayLike from 'lodash-es/isArrayLike'; +import rest from 'lodash-es/rest'; + +export default function _parallel(eachfn, tasks, callback) { + callback = callback || noop; + var results = isArrayLike(tasks) ? [] : {}; + + eachfn(tasks, function (task, key, callback) { + task(rest(function (err, args) { + if (args.length <= 1) { + args = args[0]; + } + results[key] = args; + callback(err); + })); + }, function (err) { + callback(err, results); + }); +} diff --git a/build/es/internal/queue.js b/build/es/internal/queue.js new file mode 100644 index 000000000..2e291e30f --- /dev/null +++ b/build/es/internal/queue.js @@ -0,0 +1,140 @@ +'use strict'; + +import arrayEach from 'lodash-es/internal/arrayEach'; +import arrayMap from 'lodash-es/internal/arrayMap'; +import isArray from 'lodash-es/isArray'; +import noop from 'lodash-es/noop'; +import property from 'lodash-es/internal/baseProperty'; + +import onlyOnce from './onlyOnce'; +import setImmediate from './setImmediate'; + +export default function queue(worker, concurrency, payload) { + if (concurrency == null) { + concurrency = 1; + } + else if(concurrency === 0) { + throw new Error('Concurrency must not be zero'); + } + function _insert(q, data, pos, callback) { + if (callback != null && typeof callback !== 'function') { + throw new Error('task callback must be a function'); + } + q.started = true; + if (!isArray(data)) { + data = [data]; + } + if(data.length === 0 && q.idle()) { + // call drain immediately if there are no tasks + return setImmediate(function() { + q.drain(); + }); + } + arrayEach(data, function(task) { + var item = { + data: task, + callback: callback || noop + }; + + if (pos) { + q.tasks.unshift(item); + } else { + q.tasks.push(item); + } + + if (q.tasks.length === q.concurrency) { + q.saturated(); + } + }); + setImmediate(q.process); + } + function _next(q, tasks) { + return function(){ + workers -= 1; + + var removed = false; + var args = arguments; + arrayEach(tasks, function (task) { + arrayEach(workersList, function (worker, index) { + if (worker === task && !removed) { + workersList.splice(index, 1); + removed = true; + } + }); + + task.callback.apply(task, args); + }); + if (q.tasks.length + workers === 0) { + q.drain(); + } + q.process(); + }; + } + + var workers = 0; + var workersList = []; + var q = { + tasks: [], + concurrency: concurrency, + payload: payload, + saturated: noop, + empty: noop, + drain: noop, + started: false, + paused: false, + push: function (data, callback) { + _insert(q, data, false, callback); + }, + kill: function () { + q.drain = noop; + q.tasks = []; + }, + unshift: function (data, callback) { + _insert(q, data, true, callback); + }, + process: function () { + while(!q.paused && workers < q.concurrency && q.tasks.length){ + + var tasks = q.payload ? + q.tasks.splice(0, q.payload) : + q.tasks.splice(0, q.tasks.length); + + var data = arrayMap(tasks, property('data')); + + if (q.tasks.length === 0) { + q.empty(); + } + workers += 1; + workersList.push(tasks[0]); + var cb = onlyOnce(_next(q, tasks)); + worker(data, cb); + } + }, + length: function () { + return q.tasks.length; + }, + running: function () { + return workers; + }, + workersList: function () { + return workersList; + }, + idle: function() { + return q.tasks.length + workers === 0; + }, + pause: function () { + q.paused = true; + }, + resume: function () { + if (q.paused === false) { return; } + q.paused = false; + var resumeCount = Math.min(q.concurrency, q.tasks.length); + // Need to call q.process once per concurrent + // worker to preserve full concurrency after pause + for (var w = 1; w <= resumeCount; w++) { + setImmediate(q.process); + } + } + }; + return q; +} diff --git a/build/es/internal/reject.js b/build/es/internal/reject.js new file mode 100644 index 000000000..8b1bf0d5c --- /dev/null +++ b/build/es/internal/reject.js @@ -0,0 +1,11 @@ +'use strict'; + +import filter from './filter'; + +export default function reject(eachfn, arr, iterator, callback) { + filter(eachfn, arr, function(value, cb) { + iterator(value, function(v) { + cb(!v); + }); + }, callback); +} diff --git a/build/es/internal/setImmediate.js b/build/es/internal/setImmediate.js new file mode 100644 index 000000000..c02ad7160 --- /dev/null +++ b/build/es/internal/setImmediate.js @@ -0,0 +1,19 @@ +'use strict'; + +var _setImmediate = typeof setImmediate === 'function' && setImmediate; + +var _delay; +if (_setImmediate) { + _delay = function(fn) { + // not a direct alias for IE10 compatibility + _setImmediate(fn); + }; +} else if (typeof process === 'object' && typeof process.nextTick === 'function') { + _delay = process.nextTick; +} else { + _delay = function(fn) { + setTimeout(fn, 0); + }; +} + +export default _delay; diff --git a/build/es/internal/withoutIndex.js b/build/es/internal/withoutIndex.js new file mode 100644 index 000000000..f163ff36f --- /dev/null +++ b/build/es/internal/withoutIndex.js @@ -0,0 +1,7 @@ +'use strict'; + +export default function _withoutIndex(iterator) { + return function (value, index, callback) { + return iterator(value, callback); + }; +} diff --git a/build/es/iterator.js b/build/es/iterator.js new file mode 100644 index 000000000..568171a1e --- /dev/null +++ b/build/es/iterator.js @@ -0,0 +1,17 @@ +'use strict'; + +export default function(tasks) { + function makeCallback(index) { + function fn() { + if (tasks.length) { + tasks[index].apply(null, arguments); + } + return fn.next(); + } + fn.next = function() { + return (index < tasks.length - 1) ? makeCallback(index + 1) : null; + }; + return fn; + } + return makeCallback(0); +} diff --git a/build/es/log.js b/build/es/log.js new file mode 100644 index 000000000..b58126412 --- /dev/null +++ b/build/es/log.js @@ -0,0 +1,5 @@ +'use strict'; + +import consoleFunc from './internal/consoleFunc'; + +export default consoleFunc('log'); diff --git a/build/es/map.js b/build/es/map.js new file mode 100644 index 000000000..2a8449c25 --- /dev/null +++ b/build/es/map.js @@ -0,0 +1,6 @@ +'use strict'; + +import doParallel from './internal/doParallel'; +import map from './internal/map'; + +export default doParallel(map); diff --git a/build/es/mapLimit.js b/build/es/mapLimit.js new file mode 100644 index 000000000..642b4c656 --- /dev/null +++ b/build/es/mapLimit.js @@ -0,0 +1,6 @@ +'use strict'; + +import doParallelLimit from './internal/doParallelLimit'; +import map from './internal/map'; + +export default doParallelLimit(map); diff --git a/build/es/mapSeries.js b/build/es/mapSeries.js new file mode 100644 index 000000000..bfcdaa23c --- /dev/null +++ b/build/es/mapSeries.js @@ -0,0 +1,6 @@ +'use strict'; + +import map from './internal/map'; +import doSeries from './internal/doSeries'; + +export default doSeries(map); diff --git a/build/es/memoize.js b/build/es/memoize.js new file mode 100644 index 000000000..184f0d6a0 --- /dev/null +++ b/build/es/memoize.js @@ -0,0 +1,36 @@ +'use strict'; + +import identity from 'lodash-es/identity'; +import rest from 'lodash-es/rest'; + +import setImmediate from './internal/setImmediate'; + +export default function memoize(fn, hasher) { + var memo = {}; + var queues = {}; + hasher = hasher || identity; + var memoized = rest(function memoized(args) { + var callback = args.pop(); + var key = hasher.apply(null, args); + if (key in memo) { + setImmediate(function() { + callback.apply(null, memo[key]); + }); + } else if (key in queues) { + queues[key].push(callback); + } else { + queues[key] = [callback]; + fn.apply(null, args.concat([rest(function(args) { + memo[key] = args; + var q = queues[key]; + delete queues[key]; + for (var i = 0, l = q.length; i < l; i++) { + q[i].apply(null, args); + } + })])); + } + }); + memoized.memo = memo; + memoized.unmemoized = fn; + return memoized; +} diff --git a/build/es/nextTick.js b/build/es/nextTick.js new file mode 100644 index 000000000..b61a8b4a9 --- /dev/null +++ b/build/es/nextTick.js @@ -0,0 +1,7 @@ +'use strict'; + +import setImmediate from './internal/setImmediate'; + +var nexTick = typeof process === 'object' && typeof process.nextTick === 'function' ? process.nextTick : setImmediate; + +export default nexTick; diff --git a/build/es/parallel.js b/build/es/parallel.js new file mode 100644 index 000000000..d3eec16a4 --- /dev/null +++ b/build/es/parallel.js @@ -0,0 +1,8 @@ +'use strict'; + +import _parallel from './internal/parallel'; +import eachOf from './eachOf'; + +export default function parallel(tasks, cb) { + return _parallel(eachOf, tasks, cb); +} diff --git a/build/es/parallelLimit.js b/build/es/parallelLimit.js new file mode 100644 index 000000000..7e66beda3 --- /dev/null +++ b/build/es/parallelLimit.js @@ -0,0 +1,8 @@ +'use strict'; + +import eachOfLimit from './internal/eachOfLimit'; +import parallel from './internal/parallel'; + +export default function parallelLimit(tasks, limit, cb) { + return parallel(eachOfLimit(limit), tasks, cb); +} diff --git a/build/es/priorityQueue.js b/build/es/priorityQueue.js new file mode 100644 index 000000000..9f9d3a0d0 --- /dev/null +++ b/build/es/priorityQueue.js @@ -0,0 +1,72 @@ +'use strict'; + +import arrayEach from 'lodash-es/internal/arrayEach'; +import isArray from 'lodash-es/isArray'; +import noop from 'lodash-es/noop'; + +import setImmediate from './setImmediate'; + +import queue from './queue'; + +export default function(worker, concurrency) { + function _compareTasks(a, b) { + return a.priority - b.priority; + } + + function _binarySearch(sequence, item, compare) { + var beg = -1, + end = sequence.length - 1; + while (beg < end) { + var mid = beg + ((end - beg + 1) >>> 1); + if (compare(item, sequence[mid]) >= 0) { + beg = mid; + } else { + end = mid - 1; + } + } + return beg; + } + + function _insert(q, data, priority, callback) { + if (callback != null && typeof callback !== 'function') { + throw new Error('task callback must be a function'); + } + q.started = true; + if (!isArray(data)) { + data = [data]; + } + if (data.length === 0) { + // call drain immediately if there are no tasks + return setImmediate(function() { + q.drain(); + }); + } + arrayEach(data, function(task) { + var item = { + data: task, + priority: priority, + callback: typeof callback === 'function' ? callback : noop + }; + + q.tasks.splice(_binarySearch(q.tasks, item, _compareTasks) + 1, 0, item); + + if (q.tasks.length === q.concurrency) { + q.saturated(); + } + setImmediate(q.process); + }); + } + + // Start with a normal queue + var q = queue(worker, concurrency); + + // Override push to accept second parameter representing priority + q.push = function(data, priority, callback) { + _insert(q, data, priority, callback); + }; + + // Remove unshift function + delete q.unshift; + + return q; +} diff --git a/build/es/queue.js b/build/es/queue.js new file mode 100644 index 000000000..de2ac93bb --- /dev/null +++ b/build/es/queue.js @@ -0,0 +1,9 @@ +'use strict'; + +import queue from './internal/queue'; + +export default function (worker, concurrency) { + return queue(function (items, cb) { + worker(items[0], cb); + }, concurrency, 1); +} diff --git a/build/es/reduce.js b/build/es/reduce.js new file mode 100644 index 000000000..51b736a8b --- /dev/null +++ b/build/es/reduce.js @@ -0,0 +1,14 @@ +'use strict'; + +import eachOfSeries from './eachOfSeries'; + +export default function reduce(arr, memo, iterator, cb) { + eachOfSeries(arr, function(x, i, cb) { + iterator(memo, x, function(err, v) { + memo = v; + cb(err); + }); + }, function(err) { + cb(err, memo); + }); +} diff --git a/build/es/reduceRight.js b/build/es/reduceRight.js new file mode 100644 index 000000000..730e32080 --- /dev/null +++ b/build/es/reduceRight.js @@ -0,0 +1,10 @@ +'use strict'; + +import reduce from './reduce'; + +var slice = Array.prototype.slice; + +export default function reduceRight (arr, memo, iterator, cb) { + var reversed = slice.call(arr).reverse(); + reduce(reversed, memo, iterator, cb); +} diff --git a/build/es/reject.js b/build/es/reject.js new file mode 100644 index 000000000..fb5b87f3f --- /dev/null +++ b/build/es/reject.js @@ -0,0 +1,6 @@ +'use strict'; + +import reject from './internal/reject'; +import doParallel from './internal/doParallel'; + +export default doParallel(reject); diff --git a/build/es/rejectLimit.js b/build/es/rejectLimit.js new file mode 100644 index 000000000..7cefed66a --- /dev/null +++ b/build/es/rejectLimit.js @@ -0,0 +1,6 @@ +'use strict'; + +import reject from './internal/reject'; +import doParallelLimit from './internal/doParallelLimit'; + +export default doParallelLimit(reject); diff --git a/build/es/rejectSeries.js b/build/es/rejectSeries.js new file mode 100644 index 000000000..e02adeadd --- /dev/null +++ b/build/es/rejectSeries.js @@ -0,0 +1,6 @@ +'use strict'; + +import reject from './internal/reject'; +import doSeries from './internal/doSeries'; + +export default doSeries(reject); diff --git a/build/es/retry.js b/build/es/retry.js new file mode 100644 index 000000000..506a42424 --- /dev/null +++ b/build/es/retry.js @@ -0,0 +1,77 @@ +'use strict'; + +import series from './series'; + +export default function retry(times, task, callback) { + var DEFAULT_TIMES = 5; + var DEFAULT_INTERVAL = 0; + + var attempts = []; + + var opts = { + times: DEFAULT_TIMES, + interval: DEFAULT_INTERVAL + }; + + function parseTimes(acc, t) { + if (typeof t === 'number') { + acc.times = parseInt(t, 10) || DEFAULT_TIMES; + } else if (typeof t === 'object') { + acc.times = parseInt(t.times, 10) || DEFAULT_TIMES; + acc.interval = parseInt(t.interval, 10) || DEFAULT_INTERVAL; + } else { + throw new Error('Unsupported argument type for \'times\': ' + typeof t); + } + } + + var length = arguments.length; + if (length < 1 || length > 3) { + throw new Error('Invalid arguments - must be either (task), (task, callback), (times, task) or (times, task, callback)'); + } else if (length <= 2 && typeof times === 'function') { + callback = task; + task = times; + } + if (typeof times !== 'function') { + parseTimes(opts, times); + } + opts.callback = callback; + opts.task = task; + + function wrappedTask(wrappedCallback, wrappedResults) { + function retryAttempt(task, finalAttempt) { + return function(seriesCallback) { + task(function(err, result) { + seriesCallback(!err || finalAttempt, { + err: err, + result: result + }); + }, wrappedResults); + }; + } + + function retryInterval(interval) { + return function(seriesCallback) { + setTimeout(function() { + seriesCallback(null); + }, interval); + }; + } + + while (opts.times) { + + var finalAttempt = !(opts.times -= 1); + attempts.push(retryAttempt(opts.task, finalAttempt)); + if (!finalAttempt && opts.interval > 0) { + attempts.push(retryInterval(opts.interval)); + } + } + + series(attempts, function(done, data) { + data = data[data.length - 1]; + (wrappedCallback || opts.callback)(data.err, data.result); + }); + } + + // If a callback is passed, run this as a controll flow + return opts.callback ? wrappedTask() : wrappedTask; +} diff --git a/build/es/seq.js b/build/es/seq.js new file mode 100644 index 000000000..c9e02c18a --- /dev/null +++ b/build/es/seq.js @@ -0,0 +1,28 @@ +'use strict'; + +import noop from 'lodash-es/noop'; +import rest from 'lodash-es/rest'; +import reduce from './reduce'; + +export default function seq( /* functions... */ ) { + var fns = arguments; + return rest(function(args) { + var that = this; + + var cb = args[args.length - 1]; + if (typeof cb == 'function') { + args.pop(); + } else { + cb = noop; + } + + reduce(fns, args, function(newargs, fn, cb) { + fn.apply(that, newargs.concat([rest(function(err, nextargs) { + cb(err, nextargs); + })])); + }, + function(err, results) { + cb.apply(that, [err].concat(results)); + }); + }); +} diff --git a/build/es/series.js b/build/es/series.js new file mode 100644 index 000000000..bd8e15d7d --- /dev/null +++ b/build/es/series.js @@ -0,0 +1,8 @@ +'use strict'; + +import parallel from './internal/parallel'; +import eachOfSeries from './eachOfSeries'; + +export default function series(tasks, cb) { + return parallel(eachOfSeries, tasks, cb); +} diff --git a/build/es/setImmediate.js b/build/es/setImmediate.js new file mode 100644 index 000000000..05d9555b0 --- /dev/null +++ b/build/es/setImmediate.js @@ -0,0 +1,5 @@ +'use strict'; + +import setImmediate from './internal/setImmediate'; + +export default setImmediate; diff --git a/build/es/some.js b/build/es/some.js new file mode 100644 index 000000000..46532b782 --- /dev/null +++ b/build/es/some.js @@ -0,0 +1,8 @@ +'use strict'; + +import identity from 'lodash-es/identity'; + +import createTester from './internal/createTester'; +import eachOf from './eachOf'; + +export default createTester(eachOf, Boolean, identity); diff --git a/build/es/someLimit.js b/build/es/someLimit.js new file mode 100644 index 000000000..6c1706713 --- /dev/null +++ b/build/es/someLimit.js @@ -0,0 +1,7 @@ +'use strict'; + +import createTester from './internal/createTester'; +import eachOfLimit from './eachOfLimit'; +import identity from 'lodash-es/identity'; + +export default createTester(eachOfLimit, Boolean, identity); diff --git a/build/es/sortBy.js b/build/es/sortBy.js new file mode 100644 index 000000000..b4c8b8ee4 --- /dev/null +++ b/build/es/sortBy.js @@ -0,0 +1,23 @@ +'use strict'; + +import arrayMap from 'lodash-es/internal/arrayMap'; +import property from 'lodash-es/internal/baseProperty'; + +import map from './map'; + +export default function sortBy (arr, iterator, cb) { + map(arr, function (x, cb) { + iterator(x, function (err, criteria) { + if (err) return cb(err); + cb(null, {value: x, criteria: criteria}); + }); + }, function (err, results) { + if (err) return cb(err); + cb(null, arrayMap(results.sort(comparator), property('value'))); + }); + + function comparator(left, right) { + var a = left.criteria, b = right.criteria; + return a < b ? -1 : a > b ? 1 : 0; + } +} diff --git a/build/es/times.js b/build/es/times.js new file mode 100644 index 000000000..da2b31ff6 --- /dev/null +++ b/build/es/times.js @@ -0,0 +1,8 @@ +'use strict'; + +import map from './map'; +import range from 'lodash-es/internal/baseRange'; + +export default function (count, iterator, callback) { + map(range(0, count, 1), iterator, callback); +} diff --git a/build/es/timesLimit.js b/build/es/timesLimit.js new file mode 100644 index 000000000..9c2fecac1 --- /dev/null +++ b/build/es/timesLimit.js @@ -0,0 +1,8 @@ +'use strict'; + +import mapLimit from './mapLimit'; +import range from 'lodash-es/internal/baseRange'; + +export default function timeLimit(count, limit, iterator, cb) { + return mapLimit(range(0, count, 1), limit, iterator, cb); +} diff --git a/build/es/timesSeries.js b/build/es/timesSeries.js new file mode 100644 index 000000000..ebcb34504 --- /dev/null +++ b/build/es/timesSeries.js @@ -0,0 +1,8 @@ +'use strict'; + +import mapSeries from './mapSeries'; +import range from 'lodash-es/internal/baseRange'; + +export default function (count, iterator, callback) { + mapSeries(range(0, count, 1), iterator, callback); +} diff --git a/build/es/transform.js b/build/es/transform.js new file mode 100644 index 000000000..7b80fcad2 --- /dev/null +++ b/build/es/transform.js @@ -0,0 +1,19 @@ +'use strict'; + +import isArray from 'lodash-es/isArray'; + +import eachOf from './eachOf'; + +export default function transform (arr, memo, iterator, callback) { + if (arguments.length === 3) { + callback = iterator; + iterator = memo; + memo = isArray(arr) ? [] : {}; + } + + eachOf(arr, function(v, k, cb) { + iterator(memo, v, k, cb); + }, function(err) { + callback(err, memo); + }); +} diff --git a/build/es/unmemoize.js b/build/es/unmemoize.js new file mode 100644 index 000000000..d652e7b87 --- /dev/null +++ b/build/es/unmemoize.js @@ -0,0 +1,7 @@ +'use strict'; + +export default function unmemoize(fn) { + return function () { + return (fn.unmemoized || fn).apply(null, arguments); + }; +} diff --git a/build/es/until.js b/build/es/until.js new file mode 100644 index 000000000..f9ed2fd2f --- /dev/null +++ b/build/es/until.js @@ -0,0 +1,9 @@ +'use strict'; + +import whilst from './whilst'; + +export default function until(test, iterator, cb) { + return whilst(function() { + return !test.apply(this, arguments); + }, iterator, cb); +} diff --git a/build/es/waterfall.js b/build/es/waterfall.js new file mode 100644 index 000000000..c629bcf53 --- /dev/null +++ b/build/es/waterfall.js @@ -0,0 +1,32 @@ +'use strict'; + +import isArray from 'lodash-es/isArray'; +import noop from 'lodash-es/noop'; +import once from 'lodash-es/once'; +import rest from 'lodash-es/rest'; + +import ensureAsync from './ensureAsync'; +import iterator from './iterator'; + +export default function(tasks, cb) { + cb = once(cb || noop); + if (!isArray(tasks)) return cb(new Error('First argument to waterfall must be an array of functions')); + if (!tasks.length) return cb(); + + function wrapIterator(iterator) { + return rest(function(err, args) { + if (err) { + cb.apply(null, [err].concat(args)); + } else { + var next = iterator.next(); + if (next) { + args.push(wrapIterator(next)); + } else { + args.push(cb); + } + ensureAsync(iterator).apply(null, args); + } + }); + } + wrapIterator(iterator(tasks))(); +} diff --git a/build/es/whilst.js b/build/es/whilst.js new file mode 100644 index 000000000..2f5c2ef17 --- /dev/null +++ b/build/es/whilst.js @@ -0,0 +1,15 @@ +'use strict'; + +import noop from 'lodash-es/noop'; +import rest from 'lodash-es/rest'; + +export default function whilst(test, iterator, cb) { + cb = cb || noop; + if (!test()) return cb(null); + var next = rest(function(err, args) { + if (err) return cb(err); + if (test.apply(this, args)) return iterator(next); + cb.apply(null, [null].concat(args)); + }); + iterator(next); +} diff --git a/build/package.json b/build/package.json new file mode 100644 index 000000000..2e0b63b28 --- /dev/null +++ b/build/package.json @@ -0,0 +1,97 @@ +{ + "name": "async", + "description": "Higher-order functions and common patterns for asynchronous code", + "version": "2.0.0-alpha", + "main": "index.js", + "esnext:main": "es/index.js", + "author": "Caolan McMahon", + "repository": { + "type": "git", + "url": "https://github.com/caolan/async.git" + }, + "bugs": { + "url": "https://github.com/caolan/async/issues" + }, + "keywords": [ + "async", + "callback", + "module", + "utility" + ], + "dependencies": { + "lodash": "^4.0.0", + "lodash-es": "^4.0.0" + }, + "devDependencies": { + "babel-cli": "^6.3.17", + "babel-core": "^6.3.26", + "babel-plugin-add-module-exports": "~0.1.2", + "babel-plugin-transform-es2015-modules-commonjs": "^6.3.16", + "babel-preset-es2015": "^6.3.13", + "babelify": "^7.2.0", + "benchmark": "bestiejs/benchmark.js", + "bluebird": "^2.9.32", + "chai": "^3.1.0", + "coveralls": "^2.11.2", + "es6-promise": "^2.3.0", + "fs-extra": "^0.26.3", + "gulp": "~3.9.0", + "jscs": "^1.13.1", + "jshint": "~2.8.0", + "karma": "^0.13.2", + "karma-browserify": "^4.2.1", + "karma-firefox-launcher": "^0.1.6", + "karma-mocha": "^0.2.0", + "karma-mocha-reporter": "^1.0.2", + "mocha": "^2.2.5", + "native-promise-only": "^0.8.0-a", + "nodeunit": ">0.0.0", + "nyc": "^2.1.0", + "recursive-readdir": "^1.3.0", + "rimraf": "^2.5.0", + "rollup": "^0.25.0", + "rollup-plugin-npm": "~1.3.0", + "rsvp": "^3.0.18", + "semver": "^4.3.6", + "uglify-js": "~2.4.0", + "vinyl-buffer": "~1.0.0", + "vinyl-source-stream": "~1.1.0", + "xyz": "^0.5.0", + "yargs": "~3.9.1" + }, + "scripts": { + "coverage": "nyc npm test && nyc report", + "coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls", + "lint": "jshint lib/ test/ mocha_test/ perf/memory.js perf/suites.js perf/benchmark.js support/ gulpfile.js karma.conf.js && jscs lib/ test/ mocha_test/ perf/memory.js perf/suites.js perf/benchmark.js support/ gulpfile.js karma.conf.js", + "mocha-browser-test": "karma start", + "mocha-node-test": "mocha mocha_test/ --compilers js:babel-core/register", + "mocha-test": "npm run mocha-node-test && npm run mocha-browser-test", + "nodeunit-test": "nodeunit test/test-async.js", + "test": "npm run-script lint && npm run nodeunit-test && npm run mocha-test" + }, + "license": "MIT", + "jam": { + "main": "async.js", + "include": [ + "async.js", + "README.md", + "LICENSE" + ], + "categories": [ + "Utilities" + ] + }, + "spm": { + "main": "async.js" + }, + "volo": { + "main": "async.js", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ] + } +} diff --git a/component.json b/component.json index 5b0514974..b699237fc 100644 --- a/component.json +++ b/component.json @@ -1,17 +1,17 @@ { "name": "async", "description": "Higher-order functions and common patterns for asynchronous code", - "version": "1.5.0", + "version": "2.0.0-alpha", "keywords": [ "async", "callback", - "utility", - "module" + "module", + "utility" ], "license": "MIT", - "main": "lib/async.js", + "main": "index.js", "repository": "caolan/async", "scripts": [ - "lib/async.js" + "index.js" ] -} +} \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 000000000..0d0b270df --- /dev/null +++ b/index.js @@ -0,0 +1 @@ +module.exports = require("./build/") diff --git a/package.json b/package.json index c0fbf4db4..756ad2b7f 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,8 @@ "name": "async", "description": "Higher-order functions and common patterns for asynchronous code", "version": "2.0.0-alpha", - "main": "lib/async.js", + "main": "index.js", + "esnext:main": "es/index.js", "author": "Caolan McMahon", "repository": { "type": "git", @@ -58,11 +59,6 @@ "xyz": "^0.5.0", "yargs": "~3.9.1" }, - "files": [ - "dist/async.js", - "dist/async.min.js", - "lib" - ], "scripts": { "coverage": "nyc npm test && nyc report", "coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls", @@ -75,27 +71,27 @@ }, "license": "MIT", "jam": { - "categories": [ - "Utilities" - ], + "main": "index.js", "include": [ - "LICENSE", + "lib/async.js", "README.md", - "lib/async.js" + "LICENSE" ], - "main": "lib/async.js" + "categories": [ + "Utilities" + ] }, "spm": { - "main": "lib/async.js" + "main": "index.js" }, "volo": { + "main": "index.js", "ignore": [ "**/.*", - "bower_components", "node_modules", + "bower_components", "test", "tests" - ], - "main": "lib/async.js" + ] } -} +} \ No newline at end of file From 8a7f8642467222519cc722c3c634036fbc9d9068 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Tue, 9 Feb 2016 19:35:49 -0800 Subject: [PATCH 372/956] fix lint error --- support/es.test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/support/es.test.js b/support/es.test.js index e5acbf82c..3107e137d 100644 --- a/support/es.test.js +++ b/support/es.test.js @@ -8,9 +8,8 @@ waterfall([ constant(42), function (val, next) { async.setImmediate(function () { - console.log("blah"); next(null, val); - }) + }); } ], function (err, result) { if (err) { throw err; } From b001e4a9c3e51bf277d69b54e5776caa7099a455 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Wed, 10 Feb 2016 15:14:36 -0800 Subject: [PATCH 373/956] handle moved lodash internal methods --- build/async.js | 21 ++++++++++----------- build/async.min.js | 2 +- build/auto.js | 6 +++--- build/es/auto.js | 6 +++--- build/es/internal/consoleFunc.js | 2 +- build/es/internal/filter.js | 4 ++-- build/es/internal/queue.js | 6 +++--- build/es/priorityQueue.js | 2 +- build/es/sortBy.js | 4 ++-- build/es/times.js | 2 +- build/es/timesLimit.js | 2 +- build/es/timesSeries.js | 2 +- build/internal/consoleFunc.js | 2 +- build/internal/filter.js | 4 ++-- build/internal/queue.js | 6 +++--- build/package.json | 12 ++++++------ build/priorityQueue.js | 2 +- build/sortBy.js | 4 ++-- build/times.js | 2 +- build/timesLimit.js | 2 +- build/timesSeries.js | 2 +- index.js | 4 +++- lib/auto.js | 6 +++--- lib/internal/consoleFunc.js | 2 +- lib/internal/filter.js | 4 ++-- lib/internal/queue.js | 6 +++--- lib/priorityQueue.js | 2 +- lib/sortBy.js | 4 ++-- lib/times.js | 2 +- lib/timesLimit.js | 2 +- lib/timesSeries.js | 2 +- package.json | 4 ++-- 32 files changed, 67 insertions(+), 66 deletions(-) diff --git a/build/async.js b/build/async.js index e5d60e308..3fe1422b7 100644 --- a/build/async.js +++ b/build/async.js @@ -11,11 +11,11 @@ * @private * @param {Function} func The function to invoke. * @param {*} thisArg The `this` binding of `func`. - * @param {...*} [args] The arguments to invoke `func` with. + * @param {...*} args The arguments to invoke `func` with. * @returns {*} Returns the result of `func`. */ function apply$1(func, thisArg, args) { - var length = args ? args.length : 0; + var length = args.length; switch (length) { case 0: return func.call(thisArg); case 1: return func.call(thisArg, args[0]); @@ -49,8 +49,6 @@ * // => false */ function isObject(value) { - // Avoid a V8 JIT bug in Chrome 19-20. - // See https://code.google.com/p/v8/issues/detail?id=2291 for more details. var type = typeof value; return !!value && (type == 'object' || type == 'function'); } @@ -642,9 +640,11 @@ */ function indexKeys(object) { var length = object ? object.length : undefined; - return (isLength(length) && (isArray(object) || isString(object) || isArguments(object))) - ? baseTimes(length, String) - : null; + if (isLength(length) && + (isArray(object) || isString(object) || isArguments(object))) { + return baseTimes(length, String); + } + return null; } /** Used as references for various `Number` constants. */ @@ -961,7 +961,7 @@ } /** - * This method returns the first argument provided to it. + * This method returns the first argument given to it. * * @static * @memberOf _ @@ -1073,8 +1073,7 @@ * Gets the index at which the first occurrence of `value` is found in `array` * using [`SameValueZero`](http://ecma-international.org/ecma-262/6.0/#sec-samevaluezero) * for equality comparisons. If `fromIndex` is negative, it's used as the offset - * from the end of `array`. If `array` is sorted providing `true` for `fromIndex` - * performs a faster binary search. + * from the end of `array`. * * @static * @memberOf _ @@ -1088,7 +1087,7 @@ * _.indexOf([1, 2, 1, 2], 2); * // => 1 * - * // using `fromIndex` + * // Search from the `fromIndex`. * _.indexOf([1, 2, 1, 2], 2, 2); * // => 3 */ diff --git a/build/async.min.js b/build/async.min.js index ff40154df..1b8ab3b46 100644 --- a/build/async.min.js +++ b/build/async.min.js @@ -1,2 +1,2 @@ -!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(n.async={})}(this,function(n){"use strict";function t(n,t,r){var e=r?r.length:0;switch(e){case 0:return n.call(t);case 1:return n.call(t,r[0]);case 2:return n.call(t,r[0],r[1]);case 3:return n.call(t,r[0],r[1],r[2])}return n.apply(t,r)}function r(n){var t=typeof n;return!!n&&("object"==t||"function"==t)}function e(n){var t=r(n)?Hn.call(n):"";return t==Un||t==Cn}function u(n){if(r(n)){var t=e(n.valueOf)?n.valueOf():n;n=r(t)?t+"":t}if("string"!=typeof n)return 0===n?n:+n;n=n.replace(Qn,"");var u=Wn.test(n);return u||Gn.test(n)?Jn(n.slice(2),u?2:8):Rn.test(n)?Nn:+n}function i(n){if(!n)return 0===n?n:0;if(n=u(n),n===Kn||n===-Kn){var t=0>n?-1:1;return t*Vn}var r=n%1;return n===n?r?n-r:n:0}function o(n,r){if("function"!=typeof n)throw new TypeError(Xn);return r=Yn(void 0===r?n.length-1:i(r),0),function(){for(var e=arguments,u=-1,i=Yn(e.length-r,0),o=Array(i);++u0&&(r=t.apply(this,arguments)),1>=n&&(t=void 0),r}}function f(n){return a(2,n)}function l(){}function s(n){return function(t){return null==t?void 0:t[n]}}function p(n){return"number"==typeof n&&n>-1&&n%1==0&&nt>=n}function h(n){return null!=n&&!("function"==typeof n&&e(n))&&p(_n(n))}function y(n,t){return rt.call(n,t)||"object"==typeof n&&t in n&&null===et(n)}function m(n){return ut(Object(n))}function v(n,t){for(var r=-1,e=Array(n);++r-1&&n%1==0&&t>n}function S(n){var t=n&&n.constructor,r="function"==typeof t&&t.prototype||vt;return n===r}function j(n){var t=S(n);if(!t&&!h(n))return m(n);var r=w(n),e=!!r,u=r||[],i=u.length;for(var o in n)!y(n,o)||e&&("length"==o||E(o,i))||t&&"constructor"==o||u.push(o);return u}function L(n){var t,r=-1;if(h(n))return t=n.length,function(){return r++,t>r?r:null};var e=j(n);return t=e.length,function(){return r++,t>r?e[r]:null}}function O(n){return function(){if(null===n)throw new Error("Callback was already called.");n.apply(this,arguments),n=null}}function x(n,t,r){function e(n){o--,n?r(n):null===u&&0>=o&&r(null)}r=f(r||l),n=n||[];for(var u,i=L(n),o=0;null!=(u=i());)o+=1,t(n[u],u,O(e));0===o&&r(null)}function A(n,t,r){function e(){var o=!0;return null===i?r(null):(t(n[i],i,O(function(n){if(n)r(n);else{if(i=u(),null===i)return r(null);o?kt(e):e()}})),void(o=!1))}r=f(r||l),n=n||[];var u=L(n),i=u();e()}function I(n){return o(function(t){var e,u=t.pop();try{e=n.apply(this,t)}catch(i){return u(i)}r(e)&&"function"==typeof e.then?e.then(function(n){u(null,n)})["catch"](function(n){u(n.message?n:new Error(n))}):u(null,e)})}function T(n,t){for(var r=-1,e=n.length;++rr&&(r=St(e+r,0)),U(n,t,r)):-1}function D(n,t,r){function e(n){h.unshift(n)}function u(n){var t=C(h,n);t>=0&&h.splice(t,1)}function i(){a--,T(h.slice(),function(n){n()})}"function"==typeof arguments[1]&&(r=t,t=null),r=f(r||l);var c=j(n),a=c.length;if(!a)return r(null);t||(t=a);var s={},p=0,h=[];e(function(){a||r(null,s)}),T(c,function(c){function a(){return t>p&&!y(s,c)&&z(v,function(n){return y(s,n)})}function f(){a()&&(p++,u(f),h[h.length-1](m,s))}for(var l,h=lt(n[c])?n[c]:[n[c]],m=o(function(n,t){if(p--,t.length<=1&&(t=t[0]),n){var e={};F(s,function(n,t){e[t]=n}),e[c]=t,r(n,e)}else s[c]=t,kt(i)}),v=h.slice(0,h.length-1),d=v.length;d--;){if(!(l=n[v[d]]))throw new Error("Has inexistant dependency");if(lt(l)&&C(l,c)>=0)throw new Error("Has cyclic dependencies")}a()?(p++,h[h.length-1](m,s)):e(f)})}function H(n,t){for(var r=-1,e=n.length,u=Array(e);++r=t;t++)kt(c.process)}}};return c}function Q(n,t){return N(n,1,t)}function R(n,t,r,e){A(n,function(n,e,u){r(t,n,function(n,r){t=r,u(n)})},function(n){e(n,t)})}function W(){var n=arguments;return o(function(t){var r=this,e=t[t.length-1];"function"==typeof e?t.pop():e=l,R(n,t,function(n,t,e){t.apply(r,n.concat([o(function(n,t){e(n,t)})]))},function(n,t){e.apply(r,[n].concat(t))})})}function G(){return W.apply(null,jt.call(arguments))}function J(n,t,r,e){var u=[];n(t,function(n,t,e){r(n,function(n,t){u=u.concat(t||[]),e(n)})},function(n){e(n,u)})}function K(n){return function(t,r,e){return n(x,t,r,e)}}function V(n){return function(t,r,e){return n(A,t,r,e)}}function X(n,t,r){return function(e,u,i,o){function c(){o&&o(r(!1,void 0))}function a(n,e,u){return o?void i(n,function(e){o&&t(e)&&(o(r(!0,n)),o=i=!1),u()}):u()}arguments.length>3?n(e,u,a,c):(o=i,i=u,n(e,a,c))}}function Y(n,t){return t}function Z(n){return function(t,r,e){e=f(e||l),t=t||[];var u=L(t);if(0>=n)return e(null);var i=!1,o=0,c=!1;!function a(){if(i&&0>=o)return e(null);for(;n>o&&!c;){var f=u();if(null===f)return i=!0,void(0>=o&&e(null));o+=1,r(t[f],f,O(function(n){o-=1,n?(e(n),c=!0):a()}))}}()}}function _(n,t,r,e){Z(t)(n,r,e)}function nn(n){return o(function(t,r){t.apply(null,r.concat([o(function(t,r){"object"==typeof console&&(t?console.error&&console.error(t):console[n]&&T(r,function(t){console[n](t)}))})]))})}function tn(n,t,r){r=r||l;var e=o(function(t,e){t?r(t):(e.push(u),n.apply(this,e))}),u=function(n,u){return n?r(n):u?void t(e):r(null)};n(u)}function rn(n,t,r){var e=0;tn(function(n){return e++<1?n(null,!0):void t.apply(this,arguments)},n,r)}function en(n,t,r){if(r=r||l,!n())return r(null);var e=o(function(u,i){return u?r(u):n.apply(this,i)?t(e):void r.apply(null,[null].concat(i))});t(e)}function un(n,t,r){var e=0;return en(function(){return++e<=1||t.apply(this,arguments)},n,r)}function on(n,t,r){return un(n,function(){return!t.apply(this,arguments)},r)}function cn(n){return function(t,r,e){return n(t,e)}}function an(n,t,r){return x(n,cn(t),r)}function fn(n,t,r,e){return Z(t)(n,cn(r),e)}function ln(n,t,r){return A(n,cn(t),r)}function sn(n){return o(function(t){var r=t.pop(),e=!0;t.push(function(){var n=arguments;e?kt(function(){r.apply(null,n)}):r.apply(null,n)}),n.apply(this,t),e=!1})}function pn(n){return!n}function hn(n,t,r,e){var u=[];n(t,function(n,t,e){r(n,function(r){r&&u.push({index:t,value:n}),e()})},function(){e(H(u.sort(function(n,t){return n.index-t.index}),s("value")))})}function yn(n){return function(t,r,e,u){return n(Z(r),t,e,u)}}function mn(n,t){function r(n){return n?e(n):void u(r)}var e=O(t||l),u=sn(n);r()}function vn(n){function t(r){function e(){return n.length&&n[r].apply(null,arguments),e.next()}return e.next=function(){return ru;u++)t[u].apply(null,n)})])))});return u.memo=r,u.unmemoized=n,u}function kn(n,t,r){r=r||l;var e=h(t)?[]:{};n(t,function(n,t,r){n(o(function(n,u){u.length<=1&&(u=u[0]),e[t]=u,r(n)}))},function(n){r(n,e)})}function bn(n,t){return kn(x,n,t)}function wn(n,t,r){return kn(Z(t),n,r)}function En(n,t){return N(function(t,r){n(t[0],r)},t,1)}function Sn(n,t){function r(n,t){return n.priority-t.priority}function e(n,t,r){for(var e=-1,u=n.length-1;u>e;){var i=e+(u-e+1>>>1);r(t,n[i])>=0?e=i:u=i-1}return e}function u(n,t,u,i){if(null!=i&&"function"!=typeof i)throw new Error("task callback must be a function");return n.started=!0,lt(t)||(t=[t]),0===t.length?kt(function(){n.drain()}):void T(t,function(t){var o={data:t,priority:u,callback:"function"==typeof i?i:l};n.tasks.splice(e(n.tasks,o,r)+1,0,o),n.tasks.length===n.concurrency&&n.saturated(),kt(n.process)})}var i=En(n,t);return i.push=function(n,t,r){u(i,n,t,r)},delete i.unshift,i}function jn(n,t,r,e){var u=Nt.call(n).reverse();R(u,t,r,e)}function Ln(n,t,r,e){hn(n,t,function(n,t){r(n,function(n){t(!n)})},e)}function On(n,t){return kn(A,n,t)}function xn(n,t,r){function e(n,t){if("number"==typeof t)n.times=parseInt(t,10)||i;else{if("object"!=typeof t)throw new Error("Unsupported argument type for 'times': "+typeof t);n.times=parseInt(t.times,10)||i,n.interval=parseInt(t.interval,10)||o}}function u(n,t){function r(n,r){return function(e){n(function(n,t){e(!n||r,{err:n,result:t})},t)}}function e(n){return function(t){setTimeout(function(){t(null)},n)}}for(;a.times;){var u=!(a.times-=1);c.push(r(a.task,u)),!u&&a.interval>0&&c.push(e(a.interval))}On(c,function(t,r){r=r[r.length-1],(n||a.callback)(r.err,r.result)})}var i=5,o=0,c=[],a={times:i,interval:o},f=arguments.length;if(1>f||f>3)throw new Error("Invalid arguments - must be either (task), (task, callback), (times, task) or (times, task, callback)");return 2>=f&&"function"==typeof n&&(r=t,t=n),"function"!=typeof n&&e(a,n),a.callback=r,a.task=t,a.callback?u():u}function An(n,t,r){function e(n,t){var r=n.criteria,e=t.criteria;return e>r?-1:r>e?1:0}Ut(n,function(n,r){t(n,function(t,e){return t?r(t):void r(null,{value:n,criteria:e})})},function(n,t){return n?r(n):void r(null,H(t.sort(e),s("value")))})}function In(n,t,r,e){for(var u=-1,i=Vt(Kt((t-n)/(r||1)),0),o=Array(i);i--;)o[e?i:++u]=n,n+=r;return o}function Tn(n,t,r){Ut(In(0,n,1),t,r)}function zn(n,t,r,e){return Ct(In(0,n,1),t,r,e)}function Mn(n,t,r){Dt(In(0,n,1),t,r)}function $n(n,t,r,e){3===arguments.length&&(e=r,r=t,t=lt(n)?[]:{}),x(n,function(n,e,u){r(t,n,e,u)},function(n){e(n,t)})}function qn(n){return function(){return(n.unmemoized||n).apply(null,arguments)}}function Bn(n,t,r){return en(function(){return!n.apply(this,arguments)},t,r)}function Fn(n,t){function r(n){return o(function(e,u){if(e)t.apply(null,[e].concat(u));else{var i=n.next();i?u.push(r(i)):u.push(t),sn(n).apply(null,u)}})}return t=f(t||l),lt(n)?n.length?void r(vn(n))():t():t(new Error("First argument to waterfall must be an array of functions"))}var Pn,Un="[object Function]",Cn="[object GeneratorFunction]",Dn=Object.prototype,Hn=Dn.toString,Nn=NaN,Qn=/^\s+|\s+$/g,Rn=/^[-+]0x[0-9a-f]+$/i,Wn=/^0b[01]+$/i,Gn=/^0o[0-7]+$/i,Jn=parseInt,Kn=1/0,Vn=1.7976931348623157e308,Xn="Expected a function",Yn=Math.max,Zn="Expected a function",_n=s("length"),nt=9007199254740991,tt=Object.prototype,rt=tt.hasOwnProperty,et=Object.getPrototypeOf,ut=Object.keys,it="[object Arguments]",ot=Object.prototype,ct=ot.hasOwnProperty,at=ot.toString,ft=ot.propertyIsEnumerable,lt=Array.isArray,st="[object String]",pt=Object.prototype,ht=pt.toString,yt=9007199254740991,mt=/^(?:0|[1-9]\d*)$/,vt=Object.prototype,dt=c(x),gt="function"==typeof setImmediate&&setImmediate;Pn=gt?function(n){gt(n)}:"object"==typeof process&&"function"==typeof process.nextTick?process.nextTick:function(n){setTimeout(n,0)};var kt=Pn,bt=c(A),wt=o(function(n,t){return o(function(r){return n.apply(null,t.concat(r))})}),Et=M(),St=Math.max,jt=Array.prototype.reverse,Lt=K(J),Ot=V(J),xt=o(function(n){var t=[null].concat(n);return function(n){return n.apply(this,t)}}),At=X(x,q,Y),It=X(_,q,Y),Tt=X(A,q,Y),zt=nn("dir"),Mt=X(x,pn,pn),$t=X(_,pn,pn),qt=K(hn),Bt=yn(hn),Ft=V(hn),Pt=nn("log"),Ut=K(dn),Ct=yn(dn),Dt=V(dn),Ht="object"==typeof process&&"function"==typeof process.nextTick?process.nextTick:kt,Nt=Array.prototype.slice,Qt=K(Ln),Rt=yn(Ln),Wt=V(Ln),Gt=X(x,Boolean,q),Jt=X(_,Boolean,q),Kt=Math.ceil,Vt=Math.max,Xt={applyEach:dt,applyEachSeries:bt,apply:wt,asyncify:I,auto:D,cargo:Q,compose:G,concat:Lt,concatSeries:Ot,constant:xt,detect:At,detectLimit:It,detectSeries:Tt,dir:zt,doDuring:rn,doUntil:on,doWhilst:un,during:tn,each:an,eachLimit:fn,eachOf:x,eachOfLimit:_,eachOfSeries:A,eachSeries:ln,ensureAsync:sn,every:Mt,everyLimit:$t,filter:qt,filterLimit:Bt,filterSeries:Ft,forever:mn,iterator:vn,log:Pt,map:Ut,mapLimit:Ct,mapSeries:Dt,memoize:gn,nextTick:Ht,parallel:bn,parallelLimit:wn,priorityQueue:Sn,queue:En,reduce:R,reduceRight:jn,reject:Qt,rejectLimit:Rt,rejectSeries:Wt,retry:xn,seq:W,series:On,setImmediate:kt,some:Gt,someLimit:Jt,sortBy:An,times:Tn,timesLimit:zn,timesSeries:Mn,transform:$n,unmemoize:qn,until:Bn,waterfall:Fn,whilst:en,all:Mt,any:Gt,forEach:an,forEachSeries:ln,forEachLimit:fn,forEachOf:x,forEachOfSeries:A,forEachOfLimit:_,inject:R,foldl:R,foldr:jn,select:qt,selectLimit:Bt,selectSeries:Ft,wrapSync:I};n["default"]=Xt,n.applyEach=dt,n.applyEachSeries=bt,n.apply=wt,n.asyncify=I,n.auto=D,n.cargo=Q,n.compose=G,n.concat=Lt,n.concatSeries=Ot,n.constant=xt,n.detect=At,n.detectLimit=It,n.detectSeries=Tt,n.dir=zt,n.doDuring=rn,n.doUntil=on,n.doWhilst=un,n.during=tn,n.each=an,n.eachLimit=fn,n.eachOf=x,n.eachOfLimit=_,n.eachOfSeries=A,n.eachSeries=ln,n.ensureAsync=sn,n.every=Mt,n.everyLimit=$t,n.filter=qt,n.filterLimit=Bt,n.filterSeries=Ft,n.forever=mn,n.iterator=vn,n.log=Pt,n.map=Ut,n.mapLimit=Ct,n.mapSeries=Dt,n.memoize=gn,n.nextTick=Ht,n.parallel=bn,n.parallelLimit=wn,n.priorityQueue=Sn,n.queue=En,n.reduce=R,n.reduceRight=jn,n.reject=Qt,n.rejectLimit=Rt,n.rejectSeries=Wt,n.retry=xn,n.seq=W,n.series=On,n.setImmediate=kt,n.some=Gt,n.someLimit=Jt,n.sortBy=An,n.times=Tn,n.timesLimit=zn,n.timesSeries=Mn,n.transform=$n,n.unmemoize=qn,n.until=Bn,n.waterfall=Fn,n.whilst=en,n.all=Mt,n.any=Gt,n.forEach=an,n.forEachSeries=ln,n.forEachLimit=fn,n.forEachOf=x,n.forEachOfSeries=A,n.forEachOfLimit=_,n.inject=R,n.foldl=R,n.foldr=jn,n.select=qt,n.selectLimit=Bt,n.selectSeries=Ft,n.wrapSync=I}); +!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t(n.async={})}(this,function(n){"use strict";function t(n,t,r){var e=r.length;switch(e){case 0:return n.call(t);case 1:return n.call(t,r[0]);case 2:return n.call(t,r[0],r[1]);case 3:return n.call(t,r[0],r[1],r[2])}return n.apply(t,r)}function r(n){var t=typeof n;return!!n&&("object"==t||"function"==t)}function e(n){var t=r(n)?Hn.call(n):"";return t==Un||t==Cn}function u(n){if(r(n)){var t=e(n.valueOf)?n.valueOf():n;n=r(t)?t+"":t}if("string"!=typeof n)return 0===n?n:+n;n=n.replace(Qn,"");var u=Wn.test(n);return u||Gn.test(n)?Jn(n.slice(2),u?2:8):Rn.test(n)?Nn:+n}function i(n){if(!n)return 0===n?n:0;if(n=u(n),n===Kn||n===-Kn){var t=0>n?-1:1;return t*Vn}var r=n%1;return n===n?r?n-r:n:0}function o(n,r){if("function"!=typeof n)throw new TypeError(Xn);return r=Yn(void 0===r?n.length-1:i(r),0),function(){for(var e=arguments,u=-1,i=Yn(e.length-r,0),o=Array(i);++u0&&(r=t.apply(this,arguments)),1>=n&&(t=void 0),r}}function f(n){return a(2,n)}function l(){}function s(n){return function(t){return null==t?void 0:t[n]}}function p(n){return"number"==typeof n&&n>-1&&n%1==0&&nt>=n}function h(n){return null!=n&&!("function"==typeof n&&e(n))&&p(_n(n))}function y(n,t){return rt.call(n,t)||"object"==typeof n&&t in n&&null===et(n)}function m(n){return ut(Object(n))}function v(n,t){for(var r=-1,e=Array(n);++r-1&&n%1==0&&t>n}function S(n){var t=n&&n.constructor,r="function"==typeof t&&t.prototype||vt;return n===r}function j(n){var t=S(n);if(!t&&!h(n))return m(n);var r=w(n),e=!!r,u=r||[],i=u.length;for(var o in n)!y(n,o)||e&&("length"==o||E(o,i))||t&&"constructor"==o||u.push(o);return u}function L(n){var t,r=-1;if(h(n))return t=n.length,function(){return r++,t>r?r:null};var e=j(n);return t=e.length,function(){return r++,t>r?e[r]:null}}function O(n){return function(){if(null===n)throw new Error("Callback was already called.");n.apply(this,arguments),n=null}}function x(n,t,r){function e(n){o--,n?r(n):null===u&&0>=o&&r(null)}r=f(r||l),n=n||[];for(var u,i=L(n),o=0;null!=(u=i());)o+=1,t(n[u],u,O(e));0===o&&r(null)}function A(n,t,r){function e(){var o=!0;return null===i?r(null):(t(n[i],i,O(function(n){if(n)r(n);else{if(i=u(),null===i)return r(null);o?kt(e):e()}})),void(o=!1))}r=f(r||l),n=n||[];var u=L(n),i=u();e()}function I(n){return o(function(t){var e,u=t.pop();try{e=n.apply(this,t)}catch(i){return u(i)}r(e)&&"function"==typeof e.then?e.then(function(n){u(null,n)})["catch"](function(n){u(n.message?n:new Error(n))}):u(null,e)})}function T(n,t){for(var r=-1,e=n.length;++rr&&(r=St(e+r,0)),U(n,t,r)):-1}function D(n,t,r){function e(n){h.unshift(n)}function u(n){var t=C(h,n);t>=0&&h.splice(t,1)}function i(){a--,T(h.slice(),function(n){n()})}"function"==typeof arguments[1]&&(r=t,t=null),r=f(r||l);var c=j(n),a=c.length;if(!a)return r(null);t||(t=a);var s={},p=0,h=[];e(function(){a||r(null,s)}),T(c,function(c){function a(){return t>p&&!y(s,c)&&z(v,function(n){return y(s,n)})}function f(){a()&&(p++,u(f),h[h.length-1](m,s))}for(var l,h=lt(n[c])?n[c]:[n[c]],m=o(function(n,t){if(p--,t.length<=1&&(t=t[0]),n){var e={};F(s,function(n,t){e[t]=n}),e[c]=t,r(n,e)}else s[c]=t,kt(i)}),v=h.slice(0,h.length-1),d=v.length;d--;){if(!(l=n[v[d]]))throw new Error("Has inexistant dependency");if(lt(l)&&C(l,c)>=0)throw new Error("Has cyclic dependencies")}a()?(p++,h[h.length-1](m,s)):e(f)})}function H(n,t){for(var r=-1,e=n.length,u=Array(e);++r=t;t++)kt(c.process)}}};return c}function Q(n,t){return N(n,1,t)}function R(n,t,r,e){A(n,function(n,e,u){r(t,n,function(n,r){t=r,u(n)})},function(n){e(n,t)})}function W(){var n=arguments;return o(function(t){var r=this,e=t[t.length-1];"function"==typeof e?t.pop():e=l,R(n,t,function(n,t,e){t.apply(r,n.concat([o(function(n,t){e(n,t)})]))},function(n,t){e.apply(r,[n].concat(t))})})}function G(){return W.apply(null,jt.call(arguments))}function J(n,t,r,e){var u=[];n(t,function(n,t,e){r(n,function(n,t){u=u.concat(t||[]),e(n)})},function(n){e(n,u)})}function K(n){return function(t,r,e){return n(x,t,r,e)}}function V(n){return function(t,r,e){return n(A,t,r,e)}}function X(n,t,r){return function(e,u,i,o){function c(){o&&o(r(!1,void 0))}function a(n,e,u){return o?void i(n,function(e){o&&t(e)&&(o(r(!0,n)),o=i=!1),u()}):u()}arguments.length>3?n(e,u,a,c):(o=i,i=u,n(e,a,c))}}function Y(n,t){return t}function Z(n){return function(t,r,e){e=f(e||l),t=t||[];var u=L(t);if(0>=n)return e(null);var i=!1,o=0,c=!1;!function a(){if(i&&0>=o)return e(null);for(;n>o&&!c;){var f=u();if(null===f)return i=!0,void(0>=o&&e(null));o+=1,r(t[f],f,O(function(n){o-=1,n?(e(n),c=!0):a()}))}}()}}function _(n,t,r,e){Z(t)(n,r,e)}function nn(n){return o(function(t,r){t.apply(null,r.concat([o(function(t,r){"object"==typeof console&&(t?console.error&&console.error(t):console[n]&&T(r,function(t){console[n](t)}))})]))})}function tn(n,t,r){r=r||l;var e=o(function(t,e){t?r(t):(e.push(u),n.apply(this,e))}),u=function(n,u){return n?r(n):u?void t(e):r(null)};n(u)}function rn(n,t,r){var e=0;tn(function(n){return e++<1?n(null,!0):void t.apply(this,arguments)},n,r)}function en(n,t,r){if(r=r||l,!n())return r(null);var e=o(function(u,i){return u?r(u):n.apply(this,i)?t(e):void r.apply(null,[null].concat(i))});t(e)}function un(n,t,r){var e=0;return en(function(){return++e<=1||t.apply(this,arguments)},n,r)}function on(n,t,r){return un(n,function(){return!t.apply(this,arguments)},r)}function cn(n){return function(t,r,e){return n(t,e)}}function an(n,t,r){return x(n,cn(t),r)}function fn(n,t,r,e){return Z(t)(n,cn(r),e)}function ln(n,t,r){return A(n,cn(t),r)}function sn(n){return o(function(t){var r=t.pop(),e=!0;t.push(function(){var n=arguments;e?kt(function(){r.apply(null,n)}):r.apply(null,n)}),n.apply(this,t),e=!1})}function pn(n){return!n}function hn(n,t,r,e){var u=[];n(t,function(n,t,e){r(n,function(r){r&&u.push({index:t,value:n}),e()})},function(){e(H(u.sort(function(n,t){return n.index-t.index}),s("value")))})}function yn(n){return function(t,r,e,u){return n(Z(r),t,e,u)}}function mn(n,t){function r(n){return n?e(n):void u(r)}var e=O(t||l),u=sn(n);r()}function vn(n){function t(r){function e(){return n.length&&n[r].apply(null,arguments),e.next()}return e.next=function(){return ru;u++)t[u].apply(null,n)})])))});return u.memo=r,u.unmemoized=n,u}function kn(n,t,r){r=r||l;var e=h(t)?[]:{};n(t,function(n,t,r){n(o(function(n,u){u.length<=1&&(u=u[0]),e[t]=u,r(n)}))},function(n){r(n,e)})}function bn(n,t){return kn(x,n,t)}function wn(n,t,r){return kn(Z(t),n,r)}function En(n,t){return N(function(t,r){n(t[0],r)},t,1)}function Sn(n,t){function r(n,t){return n.priority-t.priority}function e(n,t,r){for(var e=-1,u=n.length-1;u>e;){var i=e+(u-e+1>>>1);r(t,n[i])>=0?e=i:u=i-1}return e}function u(n,t,u,i){if(null!=i&&"function"!=typeof i)throw new Error("task callback must be a function");return n.started=!0,lt(t)||(t=[t]),0===t.length?kt(function(){n.drain()}):void T(t,function(t){var o={data:t,priority:u,callback:"function"==typeof i?i:l};n.tasks.splice(e(n.tasks,o,r)+1,0,o),n.tasks.length===n.concurrency&&n.saturated(),kt(n.process)})}var i=En(n,t);return i.push=function(n,t,r){u(i,n,t,r)},delete i.unshift,i}function jn(n,t,r,e){var u=Nt.call(n).reverse();R(u,t,r,e)}function Ln(n,t,r,e){hn(n,t,function(n,t){r(n,function(n){t(!n)})},e)}function On(n,t){return kn(A,n,t)}function xn(n,t,r){function e(n,t){if("number"==typeof t)n.times=parseInt(t,10)||i;else{if("object"!=typeof t)throw new Error("Unsupported argument type for 'times': "+typeof t);n.times=parseInt(t.times,10)||i,n.interval=parseInt(t.interval,10)||o}}function u(n,t){function r(n,r){return function(e){n(function(n,t){e(!n||r,{err:n,result:t})},t)}}function e(n){return function(t){setTimeout(function(){t(null)},n)}}for(;a.times;){var u=!(a.times-=1);c.push(r(a.task,u)),!u&&a.interval>0&&c.push(e(a.interval))}On(c,function(t,r){r=r[r.length-1],(n||a.callback)(r.err,r.result)})}var i=5,o=0,c=[],a={times:i,interval:o},f=arguments.length;if(1>f||f>3)throw new Error("Invalid arguments - must be either (task), (task, callback), (times, task) or (times, task, callback)");return 2>=f&&"function"==typeof n&&(r=t,t=n),"function"!=typeof n&&e(a,n),a.callback=r,a.task=t,a.callback?u():u}function An(n,t,r){function e(n,t){var r=n.criteria,e=t.criteria;return e>r?-1:r>e?1:0}Ut(n,function(n,r){t(n,function(t,e){return t?r(t):void r(null,{value:n,criteria:e})})},function(n,t){return n?r(n):void r(null,H(t.sort(e),s("value")))})}function In(n,t,r,e){for(var u=-1,i=Vt(Kt((t-n)/(r||1)),0),o=Array(i);i--;)o[e?i:++u]=n,n+=r;return o}function Tn(n,t,r){Ut(In(0,n,1),t,r)}function zn(n,t,r,e){return Ct(In(0,n,1),t,r,e)}function Mn(n,t,r){Dt(In(0,n,1),t,r)}function $n(n,t,r,e){3===arguments.length&&(e=r,r=t,t=lt(n)?[]:{}),x(n,function(n,e,u){r(t,n,e,u)},function(n){e(n,t)})}function qn(n){return function(){return(n.unmemoized||n).apply(null,arguments)}}function Bn(n,t,r){return en(function(){return!n.apply(this,arguments)},t,r)}function Fn(n,t){function r(n){return o(function(e,u){if(e)t.apply(null,[e].concat(u));else{var i=n.next();i?u.push(r(i)):u.push(t),sn(n).apply(null,u)}})}return t=f(t||l),lt(n)?n.length?void r(vn(n))():t():t(new Error("First argument to waterfall must be an array of functions"))}var Pn,Un="[object Function]",Cn="[object GeneratorFunction]",Dn=Object.prototype,Hn=Dn.toString,Nn=NaN,Qn=/^\s+|\s+$/g,Rn=/^[-+]0x[0-9a-f]+$/i,Wn=/^0b[01]+$/i,Gn=/^0o[0-7]+$/i,Jn=parseInt,Kn=1/0,Vn=1.7976931348623157e308,Xn="Expected a function",Yn=Math.max,Zn="Expected a function",_n=s("length"),nt=9007199254740991,tt=Object.prototype,rt=tt.hasOwnProperty,et=Object.getPrototypeOf,ut=Object.keys,it="[object Arguments]",ot=Object.prototype,ct=ot.hasOwnProperty,at=ot.toString,ft=ot.propertyIsEnumerable,lt=Array.isArray,st="[object String]",pt=Object.prototype,ht=pt.toString,yt=9007199254740991,mt=/^(?:0|[1-9]\d*)$/,vt=Object.prototype,dt=c(x),gt="function"==typeof setImmediate&&setImmediate;Pn=gt?function(n){gt(n)}:"object"==typeof process&&"function"==typeof process.nextTick?process.nextTick:function(n){setTimeout(n,0)};var kt=Pn,bt=c(A),wt=o(function(n,t){return o(function(r){return n.apply(null,t.concat(r))})}),Et=M(),St=Math.max,jt=Array.prototype.reverse,Lt=K(J),Ot=V(J),xt=o(function(n){var t=[null].concat(n);return function(n){return n.apply(this,t)}}),At=X(x,q,Y),It=X(_,q,Y),Tt=X(A,q,Y),zt=nn("dir"),Mt=X(x,pn,pn),$t=X(_,pn,pn),qt=K(hn),Bt=yn(hn),Ft=V(hn),Pt=nn("log"),Ut=K(dn),Ct=yn(dn),Dt=V(dn),Ht="object"==typeof process&&"function"==typeof process.nextTick?process.nextTick:kt,Nt=Array.prototype.slice,Qt=K(Ln),Rt=yn(Ln),Wt=V(Ln),Gt=X(x,Boolean,q),Jt=X(_,Boolean,q),Kt=Math.ceil,Vt=Math.max,Xt={applyEach:dt,applyEachSeries:bt,apply:wt,asyncify:I,auto:D,cargo:Q,compose:G,concat:Lt,concatSeries:Ot,constant:xt,detect:At,detectLimit:It,detectSeries:Tt,dir:zt,doDuring:rn,doUntil:on,doWhilst:un,during:tn,each:an,eachLimit:fn,eachOf:x,eachOfLimit:_,eachOfSeries:A,eachSeries:ln,ensureAsync:sn,every:Mt,everyLimit:$t,filter:qt,filterLimit:Bt,filterSeries:Ft,forever:mn,iterator:vn,log:Pt,map:Ut,mapLimit:Ct,mapSeries:Dt,memoize:gn,nextTick:Ht,parallel:bn,parallelLimit:wn,priorityQueue:Sn,queue:En,reduce:R,reduceRight:jn,reject:Qt,rejectLimit:Rt,rejectSeries:Wt,retry:xn,seq:W,series:On,setImmediate:kt,some:Gt,someLimit:Jt,sortBy:An,times:Tn,timesLimit:zn,timesSeries:Mn,transform:$n,unmemoize:qn,until:Bn,waterfall:Fn,whilst:en,all:Mt,any:Gt,forEach:an,forEachSeries:ln,forEachLimit:fn,forEachOf:x,forEachOfSeries:A,forEachOfLimit:_,inject:R,foldl:R,foldr:jn,select:qt,selectLimit:Bt,selectSeries:Ft,wrapSync:I};n["default"]=Xt,n.applyEach=dt,n.applyEachSeries=bt,n.apply=wt,n.asyncify=I,n.auto=D,n.cargo=Q,n.compose=G,n.concat=Lt,n.concatSeries=Ot,n.constant=xt,n.detect=At,n.detectLimit=It,n.detectSeries=Tt,n.dir=zt,n.doDuring=rn,n.doUntil=on,n.doWhilst=un,n.during=tn,n.each=an,n.eachLimit=fn,n.eachOf=x,n.eachOfLimit=_,n.eachOfSeries=A,n.eachSeries=ln,n.ensureAsync=sn,n.every=Mt,n.everyLimit=$t,n.filter=qt,n.filterLimit=Bt,n.filterSeries=Ft,n.forever=mn,n.iterator=vn,n.log=Pt,n.map=Ut,n.mapLimit=Ct,n.mapSeries=Dt,n.memoize=gn,n.nextTick=Ht,n.parallel=bn,n.parallelLimit=wn,n.priorityQueue=Sn,n.queue=En,n.reduce=R,n.reduceRight=jn,n.reject=Qt,n.rejectLimit=Rt,n.rejectSeries=Wt,n.retry=xn,n.seq=W,n.series=On,n.setImmediate=kt,n.some=Gt,n.someLimit=Jt,n.sortBy=An,n.times=Tn,n.timesLimit=zn,n.timesSeries=Mn,n.transform=$n,n.unmemoize=qn,n.until=Bn,n.waterfall=Fn,n.whilst=en,n.all=Mt,n.any=Gt,n.forEach=an,n.forEachSeries=ln,n.forEachLimit=fn,n.forEachOf=x,n.forEachOfSeries=A,n.forEachOfLimit=_,n.inject=R,n.foldl=R,n.foldr=jn,n.select=qt,n.selectLimit=Bt,n.selectSeries=Ft,n.wrapSync=I}); //# sourceMappingURL=dist/async.min.map \ No newline at end of file diff --git a/build/auto.js b/build/auto.js index 37a357523..51f7d100e 100644 --- a/build/auto.js +++ b/build/auto.js @@ -96,15 +96,15 @@ exports.default = function (tasks, concurrency, callback) { }); }; -var _arrayEach = require('lodash/internal/arrayEach'); +var _arrayEach = require('lodash/_arrayEach'); var _arrayEach2 = _interopRequireDefault(_arrayEach); -var _arrayEvery = require('lodash/internal/arrayEvery'); +var _arrayEvery = require('lodash/_arrayEvery'); var _arrayEvery2 = _interopRequireDefault(_arrayEvery); -var _baseHas = require('lodash/internal/baseHas'); +var _baseHas = require('lodash/_baseHas'); var _baseHas2 = _interopRequireDefault(_baseHas); diff --git a/build/es/auto.js b/build/es/auto.js index 19f4215f2..8b01ac53e 100644 --- a/build/es/auto.js +++ b/build/es/auto.js @@ -1,8 +1,8 @@ 'use strict'; -import arrayEach from 'lodash-es/internal/arrayEach'; -import arrayEvery from 'lodash-es/internal/arrayEvery'; -import baseHas from 'lodash-es/internal/baseHas'; +import arrayEach from 'lodash-es/_arrayEach'; +import arrayEvery from 'lodash-es/_arrayEvery'; +import baseHas from 'lodash-es/_baseHas'; import forOwn from 'lodash-es/forOwn'; import indexOf from 'lodash-es/indexOf'; import isArray from 'lodash-es/isArray'; diff --git a/build/es/internal/consoleFunc.js b/build/es/internal/consoleFunc.js index 7796d876e..d56a7bd44 100644 --- a/build/es/internal/consoleFunc.js +++ b/build/es/internal/consoleFunc.js @@ -1,6 +1,6 @@ 'use strict'; -import arrayEach from 'lodash-es/internal/arrayEach'; +import arrayEach from 'lodash-es/_arrayEach'; import rest from 'lodash-es/rest'; export default function consoleFunc(name) { diff --git a/build/es/internal/filter.js b/build/es/internal/filter.js index f58eb5226..62932a0e2 100644 --- a/build/es/internal/filter.js +++ b/build/es/internal/filter.js @@ -1,7 +1,7 @@ 'use strict'; -import arrayMap from 'lodash-es/internal/arrayMap'; -import property from 'lodash-es/internal/baseProperty'; +import arrayMap from 'lodash-es/_arrayMap'; +import property from 'lodash-es/_baseProperty'; export default function _filter(eachfn, arr, iterator, callback) { var results = []; diff --git a/build/es/internal/queue.js b/build/es/internal/queue.js index 2e291e30f..e15229bbf 100644 --- a/build/es/internal/queue.js +++ b/build/es/internal/queue.js @@ -1,10 +1,10 @@ 'use strict'; -import arrayEach from 'lodash-es/internal/arrayEach'; -import arrayMap from 'lodash-es/internal/arrayMap'; +import arrayEach from 'lodash-es/_arrayEach'; +import arrayMap from 'lodash-es/_arrayMap'; import isArray from 'lodash-es/isArray'; import noop from 'lodash-es/noop'; -import property from 'lodash-es/internal/baseProperty'; +import property from 'lodash-es/_baseProperty'; import onlyOnce from './onlyOnce'; import setImmediate from './setImmediate'; diff --git a/build/es/priorityQueue.js b/build/es/priorityQueue.js index 9f9d3a0d0..b22e96f4b 100644 --- a/build/es/priorityQueue.js +++ b/build/es/priorityQueue.js @@ -1,6 +1,6 @@ 'use strict'; -import arrayEach from 'lodash-es/internal/arrayEach'; +import arrayEach from 'lodash-es/_arrayEach'; import isArray from 'lodash-es/isArray'; import noop from 'lodash-es/noop'; diff --git a/build/es/sortBy.js b/build/es/sortBy.js index b4c8b8ee4..75d61ca3c 100644 --- a/build/es/sortBy.js +++ b/build/es/sortBy.js @@ -1,7 +1,7 @@ 'use strict'; -import arrayMap from 'lodash-es/internal/arrayMap'; -import property from 'lodash-es/internal/baseProperty'; +import arrayMap from 'lodash-es/_arrayMap'; +import property from 'lodash-es/_baseProperty'; import map from './map'; diff --git a/build/es/times.js b/build/es/times.js index da2b31ff6..a03cd0c0c 100644 --- a/build/es/times.js +++ b/build/es/times.js @@ -1,7 +1,7 @@ 'use strict'; import map from './map'; -import range from 'lodash-es/internal/baseRange'; +import range from 'lodash-es/_baseRange'; export default function (count, iterator, callback) { map(range(0, count, 1), iterator, callback); diff --git a/build/es/timesLimit.js b/build/es/timesLimit.js index 9c2fecac1..391652fc8 100644 --- a/build/es/timesLimit.js +++ b/build/es/timesLimit.js @@ -1,7 +1,7 @@ 'use strict'; import mapLimit from './mapLimit'; -import range from 'lodash-es/internal/baseRange'; +import range from 'lodash-es/_baseRange'; export default function timeLimit(count, limit, iterator, cb) { return mapLimit(range(0, count, 1), limit, iterator, cb); diff --git a/build/es/timesSeries.js b/build/es/timesSeries.js index ebcb34504..f73a43520 100644 --- a/build/es/timesSeries.js +++ b/build/es/timesSeries.js @@ -1,7 +1,7 @@ 'use strict'; import mapSeries from './mapSeries'; -import range from 'lodash-es/internal/baseRange'; +import range from 'lodash-es/_baseRange'; export default function (count, iterator, callback) { mapSeries(range(0, count, 1), iterator, callback); diff --git a/build/internal/consoleFunc.js b/build/internal/consoleFunc.js index de591e145..f7befa8ca 100644 --- a/build/internal/consoleFunc.js +++ b/build/internal/consoleFunc.js @@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", { }); exports.default = consoleFunc; -var _arrayEach = require('lodash/internal/arrayEach'); +var _arrayEach = require('lodash/_arrayEach'); var _arrayEach2 = _interopRequireDefault(_arrayEach); diff --git a/build/internal/filter.js b/build/internal/filter.js index 31ea40f76..df40b92d4 100644 --- a/build/internal/filter.js +++ b/build/internal/filter.js @@ -5,11 +5,11 @@ Object.defineProperty(exports, "__esModule", { }); exports.default = _filter; -var _arrayMap = require('lodash/internal/arrayMap'); +var _arrayMap = require('lodash/_arrayMap'); var _arrayMap2 = _interopRequireDefault(_arrayMap); -var _baseProperty = require('lodash/internal/baseProperty'); +var _baseProperty = require('lodash/_baseProperty'); var _baseProperty2 = _interopRequireDefault(_baseProperty); diff --git a/build/internal/queue.js b/build/internal/queue.js index 0c5c51bfa..72fc68591 100644 --- a/build/internal/queue.js +++ b/build/internal/queue.js @@ -5,11 +5,11 @@ Object.defineProperty(exports, "__esModule", { }); exports.default = queue; -var _arrayEach = require('lodash/internal/arrayEach'); +var _arrayEach = require('lodash/_arrayEach'); var _arrayEach2 = _interopRequireDefault(_arrayEach); -var _arrayMap = require('lodash/internal/arrayMap'); +var _arrayMap = require('lodash/_arrayMap'); var _arrayMap2 = _interopRequireDefault(_arrayMap); @@ -21,7 +21,7 @@ var _noop = require('lodash/noop'); var _noop2 = _interopRequireDefault(_noop); -var _baseProperty = require('lodash/internal/baseProperty'); +var _baseProperty = require('lodash/_baseProperty'); var _baseProperty2 = _interopRequireDefault(_baseProperty); diff --git a/build/package.json b/build/package.json index 2e0b63b28..55a457e28 100644 --- a/build/package.json +++ b/build/package.json @@ -19,8 +19,8 @@ "utility" ], "dependencies": { - "lodash": "^4.0.0", - "lodash-es": "^4.0.0" + "lodash": "^4.3.0", + "lodash-es": "^4.3.0" }, "devDependencies": { "babel-cli": "^6.3.17", @@ -71,9 +71,9 @@ }, "license": "MIT", "jam": { - "main": "async.js", + "main": "index.js", "include": [ - "async.js", + "lib/async.js", "README.md", "LICENSE" ], @@ -82,10 +82,10 @@ ] }, "spm": { - "main": "async.js" + "main": "index.js" }, "volo": { - "main": "async.js", + "main": "index.js", "ignore": [ "**/.*", "node_modules", diff --git a/build/priorityQueue.js b/build/priorityQueue.js index c42751dde..8f02f1669 100644 --- a/build/priorityQueue.js +++ b/build/priorityQueue.js @@ -67,7 +67,7 @@ exports.default = function (worker, concurrency) { return q; }; -var _arrayEach = require('lodash/internal/arrayEach'); +var _arrayEach = require('lodash/_arrayEach'); var _arrayEach2 = _interopRequireDefault(_arrayEach); diff --git a/build/sortBy.js b/build/sortBy.js index f6591a2db..0dd935f5a 100644 --- a/build/sortBy.js +++ b/build/sortBy.js @@ -5,11 +5,11 @@ Object.defineProperty(exports, "__esModule", { }); exports.default = sortBy; -var _arrayMap = require('lodash/internal/arrayMap'); +var _arrayMap = require('lodash/_arrayMap'); var _arrayMap2 = _interopRequireDefault(_arrayMap); -var _baseProperty = require('lodash/internal/baseProperty'); +var _baseProperty = require('lodash/_baseProperty'); var _baseProperty2 = _interopRequireDefault(_baseProperty); diff --git a/build/times.js b/build/times.js index 25aef5439..e8a81a57f 100644 --- a/build/times.js +++ b/build/times.js @@ -12,7 +12,7 @@ var _map = require('./map'); var _map2 = _interopRequireDefault(_map); -var _baseRange = require('lodash/internal/baseRange'); +var _baseRange = require('lodash/_baseRange'); var _baseRange2 = _interopRequireDefault(_baseRange); diff --git a/build/timesLimit.js b/build/timesLimit.js index 236b12379..96f52fee4 100644 --- a/build/timesLimit.js +++ b/build/timesLimit.js @@ -9,7 +9,7 @@ var _mapLimit = require('./mapLimit'); var _mapLimit2 = _interopRequireDefault(_mapLimit); -var _baseRange = require('lodash/internal/baseRange'); +var _baseRange = require('lodash/_baseRange'); var _baseRange2 = _interopRequireDefault(_baseRange); diff --git a/build/timesSeries.js b/build/timesSeries.js index 8681d9fc2..bf3042c8a 100644 --- a/build/timesSeries.js +++ b/build/timesSeries.js @@ -12,7 +12,7 @@ var _mapSeries = require('./mapSeries'); var _mapSeries2 = _interopRequireDefault(_mapSeries); -var _baseRange = require('lodash/internal/baseRange'); +var _baseRange = require('lodash/_baseRange'); var _baseRange2 = _interopRequireDefault(_baseRange); diff --git a/index.js b/index.js index 0d0b270df..031f69019 100644 --- a/index.js +++ b/index.js @@ -1 +1,3 @@ -module.exports = require("./build/") +// This is not the main file in the npm package, but here so we can use github +// tarballs as packages when necessary. +module.exports = require("./build/"); diff --git a/lib/auto.js b/lib/auto.js index 2b3db6e69..34678962e 100644 --- a/lib/auto.js +++ b/lib/auto.js @@ -1,8 +1,8 @@ 'use strict'; -import arrayEach from 'lodash/internal/arrayEach'; -import arrayEvery from 'lodash/internal/arrayEvery'; -import baseHas from 'lodash/internal/baseHas'; +import arrayEach from 'lodash/_arrayEach'; +import arrayEvery from 'lodash/_arrayEvery'; +import baseHas from 'lodash/_baseHas'; import forOwn from 'lodash/forOwn'; import indexOf from 'lodash/indexOf'; import isArray from 'lodash/isArray'; diff --git a/lib/internal/consoleFunc.js b/lib/internal/consoleFunc.js index 1ff702b95..fd8130dba 100644 --- a/lib/internal/consoleFunc.js +++ b/lib/internal/consoleFunc.js @@ -1,6 +1,6 @@ 'use strict'; -import arrayEach from 'lodash/internal/arrayEach'; +import arrayEach from 'lodash/_arrayEach'; import rest from 'lodash/rest'; export default function consoleFunc(name) { diff --git a/lib/internal/filter.js b/lib/internal/filter.js index 9f2ba8717..4d5ceb8e1 100644 --- a/lib/internal/filter.js +++ b/lib/internal/filter.js @@ -1,7 +1,7 @@ 'use strict'; -import arrayMap from 'lodash/internal/arrayMap'; -import property from 'lodash/internal/baseProperty'; +import arrayMap from 'lodash/_arrayMap'; +import property from 'lodash/_baseProperty'; export default function _filter(eachfn, arr, iterator, callback) { var results = []; diff --git a/lib/internal/queue.js b/lib/internal/queue.js index f6e665bea..87c99f87f 100644 --- a/lib/internal/queue.js +++ b/lib/internal/queue.js @@ -1,10 +1,10 @@ 'use strict'; -import arrayEach from 'lodash/internal/arrayEach'; -import arrayMap from 'lodash/internal/arrayMap'; +import arrayEach from 'lodash/_arrayEach'; +import arrayMap from 'lodash/_arrayMap'; import isArray from 'lodash/isArray'; import noop from 'lodash/noop'; -import property from 'lodash/internal/baseProperty'; +import property from 'lodash/_baseProperty'; import onlyOnce from './onlyOnce'; import setImmediate from './setImmediate'; diff --git a/lib/priorityQueue.js b/lib/priorityQueue.js index 6599c3eba..6cc10edbf 100644 --- a/lib/priorityQueue.js +++ b/lib/priorityQueue.js @@ -1,6 +1,6 @@ 'use strict'; -import arrayEach from 'lodash/internal/arrayEach'; +import arrayEach from 'lodash/_arrayEach'; import isArray from 'lodash/isArray'; import noop from 'lodash/noop'; diff --git a/lib/sortBy.js b/lib/sortBy.js index 8507c8940..4f1482318 100644 --- a/lib/sortBy.js +++ b/lib/sortBy.js @@ -1,7 +1,7 @@ 'use strict'; -import arrayMap from 'lodash/internal/arrayMap'; -import property from 'lodash/internal/baseProperty'; +import arrayMap from 'lodash/_arrayMap'; +import property from 'lodash/_baseProperty'; import map from './map'; diff --git a/lib/times.js b/lib/times.js index 493d3c177..72e844c88 100644 --- a/lib/times.js +++ b/lib/times.js @@ -1,7 +1,7 @@ 'use strict'; import map from './map'; -import range from 'lodash/internal/baseRange'; +import range from 'lodash/_baseRange'; export default function (count, iterator, callback) { map(range(0, count, 1), iterator, callback); diff --git a/lib/timesLimit.js b/lib/timesLimit.js index 2f3559e2e..1ea73132e 100644 --- a/lib/timesLimit.js +++ b/lib/timesLimit.js @@ -1,7 +1,7 @@ 'use strict'; import mapLimit from './mapLimit'; -import range from 'lodash/internal/baseRange'; +import range from 'lodash/_baseRange'; export default function timeLimit(count, limit, iterator, cb) { return mapLimit(range(0, count, 1), limit, iterator, cb); diff --git a/lib/timesSeries.js b/lib/timesSeries.js index 4377acb21..792727066 100644 --- a/lib/timesSeries.js +++ b/lib/timesSeries.js @@ -1,7 +1,7 @@ 'use strict'; import mapSeries from './mapSeries'; -import range from 'lodash/internal/baseRange'; +import range from 'lodash/_baseRange'; export default function (count, iterator, callback) { mapSeries(range(0, count, 1), iterator, callback); diff --git a/package.json b/package.json index 756ad2b7f..e0b56c51f 100644 --- a/package.json +++ b/package.json @@ -19,8 +19,8 @@ "utility" ], "dependencies": { - "lodash": "^4.0.0", - "lodash-es": "^4.0.0" + "lodash": "^4.3.0", + "lodash-es": "^4.3.0" }, "devDependencies": { "babel-cli": "^6.3.17", From 8423f0bec47e0f6ff3652954b5ccb303fc72aeb8 Mon Sep 17 00:00:00 2001 From: Alexander Early Date: Wed, 10 Feb 2016 16:00:17 -0800 Subject: [PATCH 374/956] use tweaked version of xyz for our publish process --- Makefile | 6 +- package.json | 3 +- support/xyz.sh | 171 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 174 insertions(+), 6 deletions(-) create mode 100755 support/xyz.sh diff --git a/Makefile b/Makefile index ca9fe841b..df798bd0e 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ PACKAGE = asyncjs REQUIRE_NAME = async BABEL_NODE = babel-node UGLIFY = uglifyjs -XYZ = xyz --repo git@github.com:caolan/async.git +XYZ = support/xyz.sh --repo git@github.com:caolan/async.git BUILDDIR = build DIST = dist @@ -23,7 +23,7 @@ CJS_BUNDLE = $(BUILDDIR)/index.js ES_MODULES = $(patsubst lib/%.js, build/es/%.js, $(JS_SRC)) -all: lint test clean build +all: clean lint build test test: npm test @@ -94,8 +94,6 @@ build: clean build-bundle build-dist build-es build-config test-build .PHONY: release-major release-minor release-patch release-major release-minor release-patch: all - $(SCRIPTS)/sync-package-managers.js - git add --force *.json git add --force $(BUILDDIR) git commit -am "update minified build"; true $(XYZ) --increment $(@:release-%=%) diff --git a/package.json b/package.json index e0b56c51f..0242e49a0 100644 --- a/package.json +++ b/package.json @@ -56,7 +56,6 @@ "uglify-js": "~2.4.0", "vinyl-buffer": "~1.0.0", "vinyl-source-stream": "~1.1.0", - "xyz": "^0.5.0", "yargs": "~3.9.1" }, "scripts": { @@ -94,4 +93,4 @@ "tests" ] } -} \ No newline at end of file +} diff --git a/support/xyz.sh b/support/xyz.sh new file mode 100755 index 000000000..fe3f2ece7 --- /dev/null +++ b/support/xyz.sh @@ -0,0 +1,171 @@ +#!/usr/bin/env bash + +# Slightly modified version of xyz that publishes from the build directory, +# rather than the root. + +set -e + +usage=" +Usage: xyz [options] + +Publish a new version of the npm package in the current working directory. +This involves updating the version number in package.json, committing this +change (along with any staged changes), tagging the commit, pushing to the +remote git repository, and finally publishing to the public npm registry. + +If present, component.json is updated along with package.json. + +Options: + +-b --branch + Specify the branch from which new versions must be published. + xyz aborts if run from any other branch to prevent accidental + publication of feature branches. 'master' is assumed if this + option is omitted. + +-i --increment + Specify the level of the current version number to increment. + Valid levels: 'major', 'minor', 'patch', 'premajor', 'preminor', + 'prepatch', and 'prerelease'. 'patch' is assumed if this option + is omitted. + +-m --message