diff options
author | U-Zyn Chua <chua@uzyn.com> | 2015-06-28 15:23:18 +0800 |
---|---|---|
committer | U-Zyn Chua <chua@uzyn.com> | 2015-06-28 15:23:18 +0800 |
commit | 57649026fe339aea13ec8d9be110d885c630a7e3 (patch) | |
tree | cfc76c9b7a7e7bda3ad50aa89c837e84d8185adc /lib/async.js | |
parent | ea3576b6da334710d0da7446064e9acc0766136a (diff) | |
parent | e7948013f3c3741606159e0c86f03dbb9e20ff2b (diff) | |
download | async-57649026fe339aea13ec8d9be110d885c630a7e3.tar.gz |
Merged from master
Diffstat (limited to 'lib/async.js')
-rw-r--r-- | lib/async.js | 69 |
1 files changed, 40 insertions, 29 deletions
diff --git a/lib/async.js b/lib/async.js index 6cbe301..9ada840 100644 --- a/lib/async.js +++ b/lib/async.js @@ -11,17 +11,14 @@ function noop() {} // global on the server, window in the browser - var root, previous_async; + var previous_async; - if (typeof window == 'object' && this === window) { - root = window; - } - else if (typeof global == 'object' && this === global) { - root = global; - } - else { - root = this; - } + // Establish the root object, `window` (`self`) in the browser, `global` + // on the server, or `this` in some virtual machines. We use `self` + // instead of `window` for `WebWorker` support. + var root = typeof self === 'object' && self.self === self && self || + typeof global === 'object' && global.global === global && global || + this; if (root != null) { previous_async = root.async; @@ -416,24 +413,11 @@ async.filterSeries = doSeries(_filter); function _reject(eachfn, arr, iterator, callback) { - var results = []; - arr = _map(arr, function (x, i) { - return {index: i, value: x}; - }); - eachfn(arr, function (x, index, callback) { - iterator(x.value, function (v) { - if (!v) { - results.push(x); - } - callback(); + _filter(eachfn, arr, function(value, cb) { + iterator(value, function(v) { + cb(!v); }); - }, function () { - callback(_map(results.sort(function (a, b) { - return a.index - b.index; - }), function (x) { - return x.value; - })); - }); + }, callback); } async.reject = doParallel(_reject); async.rejectSeries = doSeries(_reject); @@ -801,6 +785,7 @@ async.concatSeries = doSeries(_concat); async.whilst = function (test, iterator, callback) { + callback = callback || noop; if (test()) { iterator(function (err) { if (err) { @@ -815,6 +800,7 @@ }; async.doWhilst = function (iterator, test, callback) { + callback = callback || noop; iterator(function (err) { if (err) { return callback(err); @@ -830,6 +816,7 @@ }; async.until = function (test, iterator, callback) { + callback = callback || noop; if (!test()) { iterator(function (err) { if (err) { @@ -844,6 +831,7 @@ }; async.doUntil = function (iterator, test, callback) { + callback = callback || noop; iterator(function (err) { if (err) { return callback(err); @@ -949,6 +937,7 @@ var q = { tasks: [], concurrency: concurrency, + payload: payload, saturated: noop, empty: noop, drain: noop, @@ -967,8 +956,8 @@ process: function () { if (!q.paused && workers < q.concurrency && q.tasks.length) { while(workers < q.concurrency && q.tasks.length){ - var tasks = payload ? - q.tasks.splice(0, payload) : + var tasks = q.payload ? + q.tasks.splice(0, q.payload) : q.tasks.splice(0, q.tasks.length); var data = _map(tasks, function (task) { @@ -1259,6 +1248,28 @@ async.ensureAsync = ensureAsync; + async.constant = function constant(/*values...*/) { + var args = [null].concat(_baseSlice(arguments)); + return function (callback) { + return callback.apply(this, args); + }; + }; + + async.wrapSync = + async.asyncify = function asyncify(func) { + return function (/*args..., callback*/) { + var args = _baseSlice(arguments); + var callback = args.pop(); + var result; + try { + result = func.apply(this, args); + } catch (e) { + return callback(e); + } + callback(null, result); + }; + }; + // Node.js if (typeof module !== 'undefined' && module.exports) { module.exports = async; |