summaryrefslogtreecommitdiff
path: root/lib/async.js
diff options
context:
space:
mode:
Diffstat (limited to 'lib/async.js')
-rw-r--r--lib/async.js63
1 files changed, 37 insertions, 26 deletions
diff --git a/lib/async.js b/lib/async.js
index f176085..e171151 100644
--- a/lib/async.js
+++ b/lib/async.js
@@ -9,6 +9,12 @@
var async = {};
function noop() {}
+ function identity(v) {
+ return v;
+ }
+ function notId(v) {
+ return !v;
+ }
// global on the server, window in the browser
var previous_async;
@@ -449,35 +455,40 @@
async.detect = doParallel(_detect);
async.detectSeries = doSeries(_detect);
+ function _createTester(eachfn, check, defaultValue) {
+ return function(arr, limit, iterator, cb) {
+ function done() {
+ if (cb) cb(defaultValue);
+ }
+ function iteratee(x, _, callback) {
+ if (!cb) return callback();
+ iterator(x, function (v) {
+ if (cb && check(v)) {
+ cb(!defaultValue);
+ cb = iterator = false;
+ }
+ callback();
+ });
+ }
+ if (arguments.length > 3) {
+ eachfn(arr, limit, iteratee, done);
+ } else {
+ cb = iterator;
+ iterator = limit;
+ eachfn(arr, iteratee, done);
+ }
+ };
+ }
+
async.any =
- async.some = function (arr, iterator, main_callback) {
- async.eachOf(arr, function (x, _, callback) {
- iterator(x, function (v) {
- if (v) {
- main_callback(true);
- main_callback = noop;
- }
- callback();
- });
- }, function () {
- main_callback(false);
- });
- };
+ async.some = _createTester(async.eachOf, identity, false);
+
+ async.someLimit = _createTester(async.eachOfLimit, identity, false);
async.all =
- async.every = function (arr, iterator, main_callback) {
- async.eachOf(arr, function (x, _, callback) {
- iterator(x, function (v) {
- if (!v) {
- main_callback(false);
- main_callback = noop;
- }
- callback();
- });
- }, function () {
- main_callback(true);
- });
- };
+ async.every = _createTester(async.eachOf, notId, true);
+
+ async.everyLimit = _createTester(async.eachOfLimit, notId, true);
async.sortBy = function (arr, iterator, callback) {
async.map(arr, function (x, callback) {