1 /*global setImmediate: false, setTimeout: false, console: false */
6 // global on the server, window in the browser
7 var root, previous_async;
11 previous_async = root.async;
14 async.noConflict = function () {
15 root.async = previous_async;
19 function only_once(fn) {
22 if (called) throw new Error("Callback was already called.");
24 fn.apply(root, arguments);
28 //// cross-browser compatiblity functions ////
30 var _each = function (arr, iterator) {
32 return arr.forEach(iterator);
34 for (var i = 0; i < arr.length; i += 1) {
35 iterator(arr[i], i, arr);
39 var _map = function (arr, iterator) {
41 return arr.map(iterator);
44 _each(arr, function (x, i, a) {
45 results.push(iterator(x, i, a));
50 var _reduce = function (arr, iterator, memo) {
52 return arr.reduce(iterator, memo);
54 _each(arr, function (x, i, a) {
55 memo = iterator(memo, x, i, a);
60 var _keys = function (obj) {
62 return Object.keys(obj);
66 if (obj.hasOwnProperty(k)) {
73 //// exported async module functions ////
75 //// nextTick implementation with browser-compatible fallback ////
76 if (typeof process === 'undefined' || !(process.nextTick)) {
77 if (typeof setImmediate === 'function') {
78 async.nextTick = function (fn) {
79 // not a direct alias for IE10 compatibility
82 async.setImmediate = async.nextTick;
85 async.nextTick = function (fn) {
88 async.setImmediate = async.nextTick;
92 async.nextTick = process.nextTick;
93 if (typeof setImmediate !== 'undefined') {
94 async.setImmediate = function (fn) {
95 // not a direct alias for IE10 compatibility
100 async.setImmediate = async.nextTick;
104 async.each = function (arr, iterator, callback) {
105 callback = callback || function () {};
110 _each(arr, function (x) {
111 iterator(x, only_once(function (err) {
114 callback = function () {};
118 if (completed >= arr.length) {
125 async.forEach = async.each;
127 async.eachSeries = function (arr, iterator, callback) {
128 callback = callback || function () {};
133 var iterate = function () {
134 iterator(arr[completed], function (err) {
137 callback = function () {};
141 if (completed >= arr.length) {
152 async.forEachSeries = async.eachSeries;
154 async.eachLimit = function (arr, limit, iterator, callback) {
155 var fn = _eachLimit(limit);
156 fn.apply(null, [arr, iterator, callback]);
158 async.forEachLimit = async.eachLimit;
160 var _eachLimit = function (limit) {
162 return function (arr, iterator, callback) {
163 callback = callback || function () {};
164 if (!arr.length || limit <= 0) {
171 (function replenish () {
172 if (completed >= arr.length) {
176 while (running < limit && started < arr.length) {
179 iterator(arr[started - 1], function (err) {
182 callback = function () {};
187 if (completed >= arr.length) {
201 var doParallel = function (fn) {
203 var args = Array.prototype.slice.call(arguments);
204 return fn.apply(null, [async.each].concat(args));
207 var doParallelLimit = function(limit, fn) {
209 var args = Array.prototype.slice.call(arguments);
210 return fn.apply(null, [_eachLimit(limit)].concat(args));
213 var doSeries = function (fn) {
215 var args = Array.prototype.slice.call(arguments);
216 return fn.apply(null, [async.eachSeries].concat(args));
221 var _asyncMap = function (eachfn, arr, iterator, callback) {
223 arr = _map(arr, function (x, i) {
224 return {index: i, value: x};
226 eachfn(arr, function (x, callback) {
227 iterator(x.value, function (err, v) {
228 results[x.index] = v;
232 callback(err, results);
235 async.map = doParallel(_asyncMap);
236 async.mapSeries = doSeries(_asyncMap);
237 async.mapLimit = function (arr, limit, iterator, callback) {
238 return _mapLimit(limit)(arr, iterator, callback);
241 var _mapLimit = function(limit) {
242 return doParallelLimit(limit, _asyncMap);
245 // reduce only has a series version, as doing reduce in parallel won't
246 // work in many situations.
247 async.reduce = function (arr, memo, iterator, callback) {
248 async.eachSeries(arr, function (x, callback) {
249 iterator(memo, x, function (err, v) {
258 async.inject = async.reduce;
260 async.foldl = async.reduce;
262 async.reduceRight = function (arr, memo, iterator, callback) {
263 var reversed = _map(arr, function (x) {
266 async.reduce(reversed, memo, iterator, callback);
269 async.foldr = async.reduceRight;
271 var _filter = function (eachfn, arr, iterator, callback) {
273 arr = _map(arr, function (x, i) {
274 return {index: i, value: x};
276 eachfn(arr, function (x, callback) {
277 iterator(x.value, function (v) {
284 callback(_map(results.sort(function (a, b) {
285 return a.index - b.index;
291 async.filter = doParallel(_filter);
292 async.filterSeries = doSeries(_filter);
294 async.select = async.filter;
295 async.selectSeries = async.filterSeries;
297 var _reject = function (eachfn, arr, iterator, callback) {
299 arr = _map(arr, function (x, i) {
300 return {index: i, value: x};
302 eachfn(arr, function (x, callback) {
303 iterator(x.value, function (v) {
310 callback(_map(results.sort(function (a, b) {
311 return a.index - b.index;
317 async.reject = doParallel(_reject);
318 async.rejectSeries = doSeries(_reject);
320 var _detect = function (eachfn, arr, iterator, main_callback) {
321 eachfn(arr, function (x, callback) {
322 iterator(x, function (result) {
325 main_callback = function () {};
335 async.detect = doParallel(_detect);
336 async.detectSeries = doSeries(_detect);
338 async.some = function (arr, iterator, main_callback) {
339 async.each(arr, function (x, callback) {
340 iterator(x, function (v) {
343 main_callback = function () {};
348 main_callback(false);
352 async.any = async.some;
354 async.every = function (arr, iterator, main_callback) {
355 async.each(arr, function (x, callback) {
356 iterator(x, function (v) {
358 main_callback(false);
359 main_callback = function () {};
368 async.all = async.every;
370 async.sortBy = function (arr, iterator, callback) {
371 async.map(arr, function (x, callback) {
372 iterator(x, function (err, criteria) {
377 callback(null, {value: x, criteria: criteria});
380 }, function (err, results) {
382 return callback(err);
385 var fn = function (left, right) {
386 var a = left.criteria, b = right.criteria;
387 return a < b ? -1 : a > b ? 1 : 0;
389 callback(null, _map(results.sort(fn), function (x) {
396 async.auto = function (tasks, callback) {
397 callback = callback || function () {};
398 var keys = _keys(tasks);
400 return callback(null);
406 var addListener = function (fn) {
407 listeners.unshift(fn);
409 var removeListener = function (fn) {
410 for (var i = 0; i < listeners.length; i += 1) {
411 if (listeners[i] === fn) {
412 listeners.splice(i, 1);
417 var taskComplete = function () {
418 _each(listeners.slice(0), function (fn) {
423 addListener(function () {
424 if (_keys(results).length === keys.length) {
425 callback(null, results);
426 callback = function () {};
430 _each(keys, function (k) {
431 var task = (tasks[k] instanceof Function) ? [tasks[k]]: tasks[k];
432 var taskCallback = function (err) {
433 var args = Array.prototype.slice.call(arguments, 1);
434 if (args.length <= 1) {
438 var safeResults = {};
439 _each(_keys(results), function(rkey) {
440 safeResults[rkey] = results[rkey];
442 safeResults[k] = args;
443 callback(err, safeResults);
444 // stop subsequent errors hitting callback multiple times
445 callback = function () {};
449 async.setImmediate(taskComplete);
452 var requires = task.slice(0, Math.abs(task.length - 1)) || [];
453 var ready = function () {
454 return _reduce(requires, function (a, x) {
455 return (a && results.hasOwnProperty(x));
456 }, true) && !results.hasOwnProperty(k);
459 task[task.length - 1](taskCallback, results);
462 var listener = function () {
464 removeListener(listener);
465 task[task.length - 1](taskCallback, results);
468 addListener(listener);
473 async.waterfall = function (tasks, callback) {
474 callback = callback || function () {};
475 if (tasks.constructor !== Array) {
476 var err = new Error('First argument to waterfall must be an array of functions');
477 return callback(err);
482 var wrapIterator = function (iterator) {
483 return function (err) {
485 callback.apply(null, arguments);
486 callback = function () {};
489 var args = Array.prototype.slice.call(arguments, 1);
490 var next = iterator.next();
492 args.push(wrapIterator(next));
497 async.setImmediate(function () {
498 iterator.apply(null, args);
503 wrapIterator(async.iterator(tasks))();
506 var _parallel = function(eachfn, tasks, callback) {
507 callback = callback || function () {};
508 if (tasks.constructor === Array) {
509 eachfn.map(tasks, function (fn, callback) {
512 var args = Array.prototype.slice.call(arguments, 1);
513 if (args.length <= 1) {
516 callback.call(null, err, args);
523 eachfn.each(_keys(tasks), function (k, callback) {
524 tasks[k](function (err) {
525 var args = Array.prototype.slice.call(arguments, 1);
526 if (args.length <= 1) {
533 callback(err, results);
538 async.parallel = function (tasks, callback) {
539 _parallel({ map: async.map, each: async.each }, tasks, callback);
542 async.parallelLimit = function(tasks, limit, callback) {
543 _parallel({ map: _mapLimit(limit), each: _eachLimit(limit) }, tasks, callback);
546 async.series = function (tasks, callback) {
547 callback = callback || function () {};
548 if (tasks.constructor === Array) {
549 async.mapSeries(tasks, function (fn, callback) {
552 var args = Array.prototype.slice.call(arguments, 1);
553 if (args.length <= 1) {
556 callback.call(null, err, args);
563 async.eachSeries(_keys(tasks), function (k, callback) {
564 tasks[k](function (err) {
565 var args = Array.prototype.slice.call(arguments, 1);
566 if (args.length <= 1) {
573 callback(err, results);
578 async.iterator = function (tasks) {
579 var makeCallback = function (index) {
580 var fn = function () {
582 tasks[index].apply(null, arguments);
586 fn.next = function () {
587 return (index < tasks.length - 1) ? makeCallback(index + 1): null;
591 return makeCallback(0);
594 async.apply = function (fn) {
595 var args = Array.prototype.slice.call(arguments, 1);
598 null, args.concat(Array.prototype.slice.call(arguments))
603 var _concat = function (eachfn, arr, fn, callback) {
605 eachfn(arr, function (x, cb) {
606 fn(x, function (err, y) {
607 r = r.concat(y || []);
614 async.concat = doParallel(_concat);
615 async.concatSeries = doSeries(_concat);
617 async.whilst = function (test, iterator, callback) {
619 iterator(function (err) {
621 return callback(err);
623 async.whilst(test, iterator, callback);
631 async.doWhilst = function (iterator, test, callback) {
632 iterator(function (err) {
634 return callback(err);
637 async.doWhilst(iterator, test, callback);
645 async.until = function (test, iterator, callback) {
647 iterator(function (err) {
649 return callback(err);
651 async.until(test, iterator, callback);
659 async.doUntil = function (iterator, test, callback) {
660 iterator(function (err) {
662 return callback(err);
665 async.doUntil(iterator, test, callback);
673 async.queue = function (worker, concurrency) {
674 if (concurrency === undefined) {
677 function _insert(q, data, pos, callback) {
678 if(data.constructor !== Array) {
681 _each(data, function(task) {
684 callback: typeof callback === 'function' ? callback : null
688 q.tasks.unshift(item);
693 if (q.saturated && q.tasks.length === concurrency) {
696 async.setImmediate(q.process);
703 concurrency: concurrency,
707 push: function (data, callback) {
708 _insert(q, data, false, callback);
710 unshift: function (data, callback) {
711 _insert(q, data, true, callback);
713 process: function () {
714 if (workers < q.concurrency && q.tasks.length) {
715 var task = q.tasks.shift();
716 if (q.empty && q.tasks.length === 0) {
720 var next = function () {
723 task.callback.apply(task, arguments);
725 if (q.drain && q.tasks.length + workers === 0) {
730 var cb = only_once(next);
731 worker(task.data, cb);
734 length: function () {
735 return q.tasks.length;
737 running: function () {
744 async.cargo = function (worker, payload) {
754 push: function (data, callback) {
755 if(data.constructor !== Array) {
758 _each(data, function(task) {
761 callback: typeof callback === 'function' ? callback : null
763 if (cargo.saturated && tasks.length === payload) {
767 async.setImmediate(cargo.process);
769 process: function process() {
771 if (tasks.length === 0) {
772 if(cargo.drain) cargo.drain();
776 var ts = typeof payload === 'number'
777 ? tasks.splice(0, payload)
780 var ds = _map(ts, function (task) {
784 if(cargo.empty) cargo.empty();
786 worker(ds, function () {
789 var args = arguments;
790 _each(ts, function (data) {
792 data.callback.apply(null, args);
799 length: function () {
802 running: function () {
809 var _console_fn = function (name) {
810 return function (fn) {
811 var args = Array.prototype.slice.call(arguments, 1);
812 fn.apply(null, args.concat([function (err) {
813 var args = Array.prototype.slice.call(arguments, 1);
814 if (typeof console !== 'undefined') {
820 else if (console[name]) {
821 _each(args, function (x) {
829 async.log = _console_fn('log');
830 async.dir = _console_fn('dir');
831 /*async.info = _console_fn('info');
832 async.warn = _console_fn('warn');
833 async.error = _console_fn('error');*/
835 async.memoize = function (fn, hasher) {
838 hasher = hasher || function (x) {
841 var memoized = function () {
842 var args = Array.prototype.slice.call(arguments);
843 var callback = args.pop();
844 var key = hasher.apply(null, args);
846 callback.apply(null, memo[key]);
848 else if (key in queues) {
849 queues[key].push(callback);
852 queues[key] = [callback];
853 fn.apply(null, args.concat([function () {
854 memo[key] = arguments;
857 for (var i = 0, l = q.length; i < l; i++) {
858 q[i].apply(null, arguments);
863 memoized.memo = memo;
864 memoized.unmemoized = fn;
868 async.unmemoize = function (fn) {
870 return (fn.unmemoized || fn).apply(null, arguments);
874 async.times = function (count, iterator, callback) {
876 for (var i = 0; i < count; i++) {
879 return async.map(counter, iterator, callback);
882 async.timesSeries = function (count, iterator, callback) {
884 for (var i = 0; i < count; i++) {
887 return async.mapSeries(counter, iterator, callback);
890 async.compose = function (/* functions... */) {
891 var fns = Array.prototype.reverse.call(arguments);
894 var args = Array.prototype.slice.call(arguments);
895 var callback = args.pop();
896 async.reduce(fns, args, function (newargs, fn, cb) {
897 fn.apply(that, newargs.concat([function () {
898 var err = arguments[0];
899 var nextargs = Array.prototype.slice.call(arguments, 1);
903 function (err, results) {
904 callback.apply(that, [err].concat(results));
909 var _applyEach = function (eachfn, fns /*args...*/) {
910 var go = function () {
912 var args = Array.prototype.slice.call(arguments);
913 var callback = args.pop();
914 return eachfn(fns, function (fn, cb) {
915 fn.apply(that, args.concat([cb]));
919 if (arguments.length > 2) {
920 var args = Array.prototype.slice.call(arguments, 2);
921 return go.apply(this, args);
927 async.applyEach = doParallel(_applyEach);
928 async.applyEachSeries = doSeries(_applyEach);
930 async.forever = function (fn, callback) {
934 return callback(err);
944 if (typeof define !== 'undefined' && define.amd) {
945 define([], function () {
950 else if (typeof module !== 'undefined' && module.exports) {
951 module.exports = async;
953 // included directly via <script> tag