summaryrefslogtreecommitdiff
path: root/dist/async.js
diff options
context:
space:
mode:
Diffstat (limited to 'dist/async.js')
-rw-r--r--dist/async.js1221
1 files changed, 670 insertions, 551 deletions
diff --git a/dist/async.js b/dist/async.js
index 4d2aa08..ddb1af7 100644
--- a/dist/async.js
+++ b/dist/async.js
@@ -1,93 +1,78 @@
(function (global, factory) {
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
- typeof define === 'function' && define.amd ? define(['exports'], factory) :
- (factory((global.async = global.async || {})));
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
+ typeof define === 'function' && define.amd ? define(['exports'], factory) :
+ (factory((global.async = 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 {Array} args The arguments to invoke `func` with.
- * @returns {*} Returns the result of `func`.
- */
-function apply(func, thisArg, args) {
- switch (args.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);
-}
-
-/* Built-in method references for those with the same name as other `lodash` methods. */
-var nativeMax = Math.max;
-
-/**
- * A specialized version of `baseRest` which transforms the rest array.
- *
- * @private
- * @param {Function} func The function to apply a rest parameter to.
- * @param {number} [start=func.length-1] The start position of the rest parameter.
- * @param {Function} transform The rest array transform.
- * @returns {Function} Returns the new function.
- */
-function overRest$1(func, start, transform) {
- start = nativeMax(start === undefined ? (func.length - 1) : start, 0);
- return function() {
- var args = arguments,
- index = -1,
- length = nativeMax(args.length - start, 0),
- array = Array(length);
-
- while (++index < length) {
- array[index] = args[start + index];
- }
- index = -1;
- var otherArgs = Array(start + 1);
- while (++index < start) {
- otherArgs[index] = args[index];
+function slice(arrayLike, start) {
+ start = start|0;
+ var newLen = Math.max(arrayLike.length - start, 0);
+ var newArr = Array(newLen);
+ for(var idx = 0; idx < newLen; idx++) {
+ newArr[idx] = arrayLike[start + idx];
}
- otherArgs[start] = transform(array);
- return apply(func, this, otherArgs);
- };
+ return newArr;
}
/**
- * This method returns the first argument it receives.
+ * 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.
+ *
+ * @name apply
* @static
- * @since 0.1.0
- * @memberOf _
+ * @memberOf module:Utils
+ * @method
* @category Util
- * @param {*} value Any value.
- * @returns {*} Returns `value`.
+ * @param {Function} fn - The function you want to eventually apply all
+ * arguments to. Invokes with (arguments...).
+ * @param {...*} arguments... - Any number of arguments to automatically apply
+ * when the continuation is called.
+ * @returns {Function} the partially-applied function
* @example
*
- * var object = { 'a': 1 };
+ * // using apply
+ * async.parallel([
+ * async.apply(fs.writeFile, 'testfile1', 'test1'),
+ * async.apply(fs.writeFile, 'testfile2', 'test2')
+ * ]);
*
- * console.log(_.identity(object) === object);
- * // => true
+ *
+ * // 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:
+ *
+ * node> var fn = async.apply(sys.puts, 'one');
+ * node> fn('two', 'three');
+ * one
+ * two
+ * three
*/
-function identity(value) {
- return value;
-}
-
-// Lodash rest function without function.toString()
-// remappings
-function rest(func, start) {
- return overRest$1(func, start, identity);
-}
+var apply = function(fn/*, ...args*/) {
+ var args = slice(arguments, 1);
+ return function(/*callArgs*/) {
+ var callArgs = slice(arguments);
+ return fn.apply(null, args.concat(callArgs));
+ };
+};
var initialParams = function (fn) {
- return rest(function (args /*..., callback*/) {
+ return function (/*...args, callback*/) {
+ var args = slice(arguments);
var callback = args.pop();
fn.call(this, args, callback);
- });
+ };
};
/**
@@ -120,6 +105,34 @@ function isObject(value) {
return value != null && (type == 'object' || type == 'function');
}
+var hasSetImmediate = typeof setImmediate === 'function' && setImmediate;
+var hasNextTick = typeof process === 'object' && typeof process.nextTick === 'function';
+
+function fallback(fn) {
+ setTimeout(fn, 0);
+}
+
+function wrap(defer) {
+ return function (fn/*, ...args*/) {
+ var args = slice(arguments, 1);
+ defer(function () {
+ fn.apply(null, args);
+ });
+ };
+}
+
+var _defer;
+
+if (hasSetImmediate) {
+ _defer = setImmediate;
+} else if (hasNextTick) {
+ _defer = process.nextTick;
+} else {
+ _defer = fallback;
+}
+
+var setImmediate$1 = wrap(_defer);
+
/**
* 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,
@@ -139,7 +152,7 @@ function isObject(value) {
* @method
* @alias wrapSync
* @category Util
- * @param {Function} func - The synchronous funuction, or Promise-returning
+ * @param {Function} func - The synchronous function, or Promise-returning
* function to convert to an {@link AsyncFunction}.
* @returns {AsyncFunction} An asynchronous wrapper of the `func`. To be
* invoked with `(args..., callback)`.
@@ -186,10 +199,10 @@ function asyncify(func) {
}
// if result is Promise object
if (isObject(result) && typeof result.then === 'function') {
- result.then(function (value) {
- callback(null, value);
- }, function (err) {
- callback(err.message ? err : new Error(err));
+ result.then(function(value) {
+ invokeCallback(callback, null, value);
+ }, function(err) {
+ invokeCallback(callback, err.message ? err : new Error(err));
});
} else {
callback(null, result);
@@ -197,19 +210,20 @@ function asyncify(func) {
});
}
-var supportsSymbol = typeof Symbol === 'function';
-
-function supportsAsync() {
- var supported;
+function invokeCallback(callback, error, value) {
try {
- /* eslint no-eval: 0 */
- supported = isAsync(eval('(async function () {})'));
+ callback(error, value);
} catch (e) {
- supported = false;
+ setImmediate$1(rethrow, e);
}
- return supported;
}
+function rethrow(error) {
+ throw error;
+}
+
+var supportsSymbol = typeof Symbol === 'function';
+
function isAsync(fn) {
return supportsSymbol && fn[Symbol.toStringTag] === 'AsyncFunction';
}
@@ -218,22 +232,22 @@ function wrapAsync(asyncFn) {
return isAsync(asyncFn) ? asyncify(asyncFn) : asyncFn;
}
-var wrapAsync$1 = supportsAsync() ? wrapAsync : identity;
-
function applyEach$1(eachfn) {
- return rest(function (fns, args) {
- var go = initialParams(function (args, callback) {
+ return function(fns/*, ...args*/) {
+ var args = slice(arguments, 1);
+ var go = initialParams(function(args, callback) {
var that = this;
return eachfn(fns, function (fn, cb) {
- wrapAsync$1(fn).apply(that, args.concat(cb));
+ wrapAsync(fn).apply(that, args.concat(cb));
}, callback);
});
if (args.length) {
return go.apply(this, args);
- } else {
+ }
+ else {
return go;
}
- });
+ };
}
/** Detect free variable `global` from Node.js. */
@@ -330,8 +344,7 @@ function baseGetTag(value) {
if (value == null) {
return value === undefined ? undefinedTag : nullTag;
}
- value = Object(value);
- return (symToStringTag && symToStringTag in value)
+ return (symToStringTag && symToStringTag in Object(value))
? getRawTag(value)
: objectToString(value);
}
@@ -740,7 +753,7 @@ var freeProcess = moduleExports$1 && freeGlobal.process;
/** Used to access faster Node.js helpers. */
var nodeUtil = (function() {
try {
- return freeProcess && freeProcess.binding('util');
+ return freeProcess && freeProcess.binding && freeProcess.binding('util');
} catch (e) {}
}());
@@ -903,18 +916,19 @@ function createArrayIterator(coll) {
var i = -1;
var len = coll.length;
return function next() {
- return ++i < len ? { value: coll[i], key: i } : null;
- };
+ return ++i < len ? {value: coll[i], key: i} : null;
+ }
}
function createES2015Iterator(iterator) {
var i = -1;
return function next() {
var item = iterator.next();
- if (item.done) return null;
+ if (item.done)
+ return null;
i++;
- return { value: item.value, key: i };
- };
+ return {value: item.value, key: i};
+ }
}
function createObjectIterator(obj) {
@@ -923,7 +937,7 @@ function createObjectIterator(obj) {
var len = okeys.length;
return function next() {
var key = okeys[++i];
- return i < len ? { value: obj[key], key: key } : null;
+ return i < len ? {value: obj[key], key: key} : null;
};
}
@@ -937,7 +951,7 @@ function iterator(coll) {
}
function onlyOnce(fn) {
- return function () {
+ return function() {
if (fn === null) throw new Error("Callback was already called.");
var callFn = fn;
fn = null;
@@ -960,15 +974,17 @@ function _eachOfLimit(limit) {
if (err) {
done = true;
callback(err);
- } else if (value === breakLoop || done && running <= 0) {
+ }
+ else if (value === breakLoop || (done && running <= 0)) {
done = true;
return callback(null);
- } else {
+ }
+ else {
replenish();
}
}
- function replenish() {
+ function replenish () {
while (running < limit && !done) {
var elem = nextElem();
if (elem === null) {
@@ -1008,7 +1024,7 @@ function _eachOfLimit(limit) {
* `iteratee` functions have finished, or an error occurs. Invoked with (err).
*/
function eachOfLimit(coll, limit, iteratee, callback) {
- _eachOfLimit(limit)(coll, wrapAsync$1(iteratee), callback);
+ _eachOfLimit(limit)(coll, wrapAsync(iteratee), callback);
}
function doLimit(fn, limit) {
@@ -1030,7 +1046,7 @@ function eachOfArrayLike(coll, iteratee, callback) {
function iteratorCallback(err, value) {
if (err) {
callback(err);
- } else if (++completed === length || value === breakLoop) {
+ } else if ((++completed === length) || value === breakLoop) {
callback(null);
}
}
@@ -1082,14 +1098,14 @@ var eachOfGeneric = doLimit(eachOfLimit, Infinity);
* doSomethingWith(configs);
* });
*/
-var eachOf = function (coll, iteratee, callback) {
+var eachOf = function(coll, iteratee, callback) {
var eachOfImplementation = isArrayLike(coll) ? eachOfArrayLike : eachOfGeneric;
- eachOfImplementation(coll, wrapAsync$1(iteratee), callback);
+ eachOfImplementation(coll, wrapAsync(iteratee), callback);
};
function doParallel(fn) {
return function (obj, iteratee, callback) {
- return fn(eachOf, obj, wrapAsync$1(iteratee), callback);
+ return fn(eachOf, obj, wrapAsync(iteratee), callback);
};
}
@@ -1098,7 +1114,7 @@ function _asyncMap(eachfn, arr, iteratee, callback) {
arr = arr || [];
var results = [];
var counter = 0;
- var _iteratee = wrapAsync$1(iteratee);
+ var _iteratee = wrapAsync(iteratee);
eachfn(arr, function (value, _, callback) {
var index = counter++;
@@ -1186,7 +1202,7 @@ var applyEach = applyEach$1(map);
function doParallelLimit(fn) {
return function (obj, limit, iteratee, callback) {
- return fn(_eachOfLimit(limit), obj, wrapAsync$1(iteratee), callback);
+ return fn(_eachOfLimit(limit), obj, wrapAsync(iteratee), callback);
};
}
@@ -1253,56 +1269,6 @@ var mapSeries = doLimit(mapLimit, 1);
var applyEachSeries = applyEach$1(mapSeries);
/**
- * 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.
- *
- * @name apply
- * @static
- * @memberOf module:Utils
- * @method
- * @category Util
- * @param {Function} function - The function you want to eventually apply all
- * arguments to. Invokes with (arguments...).
- * @param {...*} arguments... - Any number of arguments to automatically apply
- * when the continuation is called.
- * @example
- *
- * // 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:
- *
- * node> var fn = async.apply(sys.puts, 'one');
- * node> fn('two', 'three');
- * one
- * two
- * three
- */
-var apply$2 = rest(function (fn, args) {
- return rest(function (callArgs) {
- return fn.apply(null, args.concat(callArgs));
- });
-});
-
-/**
* A specialized version of `_.forEach` for arrays without support for
* iteratee shorthands.
*
@@ -1570,7 +1536,10 @@ var auto = function (tasks, concurrency, callback) {
arrayEach(dependencies, function (dependencyName) {
if (!tasks[dependencyName]) {
- throw new Error('async.auto task `' + key + '` has a non-existent dependency `' + dependencyName + '` in ' + dependencies.join(', '));
+ throw new Error('async.auto task `' + key +
+ '` has a non-existent dependency `' +
+ dependencyName + '` in ' +
+ dependencies.join(', '));
}
addListener(dependencyName, function () {
remainingDependencies--;
@@ -1594,10 +1563,11 @@ var auto = function (tasks, concurrency, callback) {
if (readyTasks.length === 0 && runningTasks === 0) {
return callback(null, results);
}
- while (readyTasks.length && runningTasks < concurrency) {
+ while(readyTasks.length && runningTasks < concurrency) {
var run = readyTasks.shift();
run();
}
+
}
function addListener(taskName, fn) {
@@ -1617,32 +1587,33 @@ var auto = function (tasks, concurrency, callback) {
processQueue();
}
+
function runTask(key, task) {
if (hasError) return;
- var taskCallback = onlyOnce(rest(function (err, args) {
+ var taskCallback = onlyOnce(function(err, result) {
runningTasks--;
- if (args.length <= 1) {
- args = args[0];
+ if (arguments.length > 2) {
+ result = slice(arguments, 1);
}
if (err) {
var safeResults = {};
- baseForOwn(results, function (val, rkey) {
+ baseForOwn(results, function(val, rkey) {
safeResults[rkey] = val;
});
- safeResults[key] = args;
+ safeResults[key] = result;
hasError = true;
listeners = Object.create(null);
callback(err, safeResults);
} else {
- results[key] = args;
+ results[key] = result;
taskComplete(key);
}
- }));
+ });
runningTasks++;
- var taskFn = wrapAsync$1(task[task.length - 1]);
+ var taskFn = wrapAsync(task[task.length - 1]);
if (task.length > 1) {
taskFn(results, taskCallback);
} else {
@@ -1667,7 +1638,9 @@ var auto = function (tasks, concurrency, callback) {
}
if (counter !== numTasks) {
- throw new Error('async.auto cannot execute tasks due to a recursive dependency');
+ throw new Error(
+ 'async.auto cannot execute tasks due to a recursive dependency'
+ );
}
}
@@ -1849,15 +1822,17 @@ function asciiToArray(string) {
/** Used to compose unicode character classes. */
var rsAstralRange = '\\ud800-\\udfff';
-var rsComboMarksRange = '\\u0300-\\u036f\\ufe20-\\ufe23';
-var rsComboSymbolsRange = '\\u20d0-\\u20f0';
+var rsComboMarksRange = '\\u0300-\\u036f';
+var reComboHalfMarksRange = '\\ufe20-\\ufe2f';
+var rsComboSymbolsRange = '\\u20d0-\\u20ff';
+var rsComboRange = rsComboMarksRange + reComboHalfMarksRange + rsComboSymbolsRange;
var rsVarRange = '\\ufe0e\\ufe0f';
/** Used to compose unicode capture groups. */
var rsZWJ = '\\u200d';
/** Used to detect strings with [zero-width joiners or code points from the astral planes](http://eev.ee/blog/2015/09/12/dark-corners-of-unicode/). */
-var reHasUnicode = RegExp('[' + rsZWJ + rsAstralRange + rsComboMarksRange + rsComboSymbolsRange + rsVarRange + ']');
+var reHasUnicode = RegExp('[' + rsZWJ + rsAstralRange + rsComboRange + rsVarRange + ']');
/**
* Checks if `string` contains Unicode symbols.
@@ -1872,13 +1847,15 @@ function hasUnicode(string) {
/** Used to compose unicode character classes. */
var rsAstralRange$1 = '\\ud800-\\udfff';
-var rsComboMarksRange$1 = '\\u0300-\\u036f\\ufe20-\\ufe23';
-var rsComboSymbolsRange$1 = '\\u20d0-\\u20f0';
+var rsComboMarksRange$1 = '\\u0300-\\u036f';
+var reComboHalfMarksRange$1 = '\\ufe20-\\ufe2f';
+var rsComboSymbolsRange$1 = '\\u20d0-\\u20ff';
+var rsComboRange$1 = rsComboMarksRange$1 + reComboHalfMarksRange$1 + rsComboSymbolsRange$1;
var rsVarRange$1 = '\\ufe0e\\ufe0f';
/** Used to compose unicode capture groups. */
var rsAstral = '[' + rsAstralRange$1 + ']';
-var rsCombo = '[' + rsComboMarksRange$1 + rsComboSymbolsRange$1 + ']';
+var rsCombo = '[' + rsComboRange$1 + ']';
var rsFitz = '\\ud83c[\\udffb-\\udfff]';
var rsModifier = '(?:' + rsCombo + '|' + rsFitz + ')';
var rsNonAstral = '[^' + rsAstralRange$1 + ']';
@@ -1995,7 +1972,7 @@ function parseParams(func) {
func = func.toString().replace(STRIP_COMMENTS, '');
func = func.match(FN_ARGS)[2].replace(' ', '');
func = func ? func.split(FN_ARG_SPLIT) : [];
- func = func.map(function (arg) {
+ func = func.map(function (arg){
return trim(arg.replace(FN_ARG, ''));
});
return func;
@@ -2089,7 +2066,9 @@ function autoInject(tasks, callback) {
baseForOwn(tasks, function (taskFn, key) {
var params;
var fnIsAsync = isAsync(taskFn);
- var hasNoDeps = !fnIsAsync && taskFn.length === 1 || fnIsAsync && taskFn.length === 0;
+ var hasNoDeps =
+ (!fnIsAsync && taskFn.length === 1) ||
+ (fnIsAsync && taskFn.length === 0);
if (isArray(taskFn)) {
params = taskFn.slice(0, -1);
@@ -2116,40 +2095,13 @@ function autoInject(tasks, callback) {
return results[name];
});
newArgs.push(taskCb);
- wrapAsync$1(taskFn).apply(null, newArgs);
+ wrapAsync(taskFn).apply(null, newArgs);
}
});
auto(newTasks, callback);
}
-var hasSetImmediate = typeof setImmediate === 'function' && setImmediate;
-var hasNextTick = typeof process === 'object' && typeof process.nextTick === 'function';
-
-function fallback(fn) {
- setTimeout(fn, 0);
-}
-
-function wrap(defer) {
- return rest(function (fn, args) {
- defer(function () {
- fn.apply(null, args);
- });
- });
-}
-
-var _defer;
-
-if (hasSetImmediate) {
- _defer = setImmediate;
-} else if (hasNextTick) {
- _defer = process.nextTick;
-} else {
- _defer = fallback;
-}
-
-var setImmediate$1 = wrap(_defer);
-
// Simple doubly linked list (https://en.wikipedia.org/wiki/Doubly_linked_list) implementation
// used for queues. This implementation assumes that the node provided by the user can be modified
// to adjust the next and last properties. We implement only the minimal functionality
@@ -2164,60 +2116,93 @@ function setInitial(dll, node) {
dll.head = dll.tail = node;
}
-DLL.prototype.removeLink = function (node) {
- if (node.prev) node.prev.next = node.next;else this.head = node.next;
- if (node.next) node.next.prev = node.prev;else this.tail = node.prev;
+DLL.prototype.removeLink = function(node) {
+ if (node.prev) node.prev.next = node.next;
+ else this.head = node.next;
+ if (node.next) node.next.prev = node.prev;
+ else this.tail = node.prev;
node.prev = node.next = null;
this.length -= 1;
return node;
};
-DLL.prototype.empty = DLL;
+DLL.prototype.empty = function () {
+ while(this.head) this.shift();
+ return this;
+};
-DLL.prototype.insertAfter = function (node, newNode) {
+DLL.prototype.insertAfter = function(node, newNode) {
newNode.prev = node;
newNode.next = node.next;
- if (node.next) node.next.prev = newNode;else this.tail = newNode;
+ if (node.next) node.next.prev = newNode;
+ else this.tail = newNode;
node.next = newNode;
this.length += 1;
};
-DLL.prototype.insertBefore = function (node, newNode) {
+DLL.prototype.insertBefore = function(node, newNode) {
newNode.prev = node.prev;
newNode.next = node;
- if (node.prev) node.prev.next = newNode;else this.head = newNode;
+ if (node.prev) node.prev.next = newNode;
+ else this.head = newNode;
node.prev = newNode;
this.length += 1;
};
-DLL.prototype.unshift = function (node) {
- if (this.head) this.insertBefore(this.head, node);else setInitial(this, node);
+DLL.prototype.unshift = function(node) {
+ if (this.head) this.insertBefore(this.head, node);
+ else setInitial(this, node);
};
-DLL.prototype.push = function (node) {
- if (this.tail) this.insertAfter(this.tail, node);else setInitial(this, node);
+DLL.prototype.push = function(node) {
+ if (this.tail) this.insertAfter(this.tail, node);
+ else setInitial(this, node);
};
-DLL.prototype.shift = function () {
+DLL.prototype.shift = function() {
return this.head && this.removeLink(this.head);
};
-DLL.prototype.pop = function () {
+DLL.prototype.pop = function() {
return this.tail && this.removeLink(this.tail);
};
+DLL.prototype.toArray = function () {
+ var arr = Array(this.length);
+ var curr = this.head;
+ for(var idx = 0; idx < this.length; idx++) {
+ arr[idx] = curr.data;
+ curr = curr.next;
+ }
+ return arr;
+};
+
+DLL.prototype.remove = function (testFn) {
+ var curr = this.head;
+ while(!!curr) {
+ var next = curr.next;
+ if (testFn(curr)) {
+ this.removeLink(curr);
+ }
+ curr = next;
+ }
+ return this;
+};
+
function queue(worker, concurrency, payload) {
if (concurrency == null) {
concurrency = 1;
- } else if (concurrency === 0) {
+ }
+ else if(concurrency === 0) {
throw new Error('Concurrency must not be zero');
}
- var _worker = wrapAsync$1(worker);
+ var _worker = wrapAsync(worker);
var numRunning = 0;
var workersList = [];
+ var processingScheduled = false;
function _insert(data, insertAtFront, callback) {
if (callback != null && typeof callback !== 'function') {
throw new Error('task callback must be a function');
@@ -2228,7 +2213,7 @@ function queue(worker, concurrency, payload) {
}
if (data.length === 0 && q.idle()) {
// call drain immediately if there are no tasks
- return setImmediate$1(function () {
+ return setImmediate$1(function() {
q.drain();
});
}
@@ -2245,28 +2230,38 @@ function queue(worker, concurrency, payload) {
q._tasks.push(item);
}
}
- setImmediate$1(q.process);
+
+ if (!processingScheduled) {
+ processingScheduled = true;
+ setImmediate$1(function() {
+ processingScheduled = false;
+ q.process();
+ });
+ }
}
function _next(tasks) {
- return rest(function (args) {
+ return function(err){
numRunning -= 1;
for (var i = 0, l = tasks.length; i < l; i++) {
var task = tasks[i];
+
var index = baseIndexOf(workersList, task, 0);
- if (index >= 0) {
- workersList.splice(index);
+ if (index === 0) {
+ workersList.shift();
+ } else if (index > 0) {
+ workersList.splice(index, 1);
}
- task.callback.apply(task, args);
+ task.callback.apply(task, arguments);
- if (args[0] != null) {
- q.error(args[0], task.data);
+ if (err != null) {
+ q.error(err, task.data);
}
}
- if (numRunning <= q.concurrency - q.buffer) {
+ if (numRunning <= (q.concurrency - q.buffer) ) {
q.unsaturated();
}
@@ -2274,7 +2269,7 @@ function queue(worker, concurrency, payload) {
q.drain();
}
q.process();
- });
+ };
}
var isProcessing = false;
@@ -2283,7 +2278,7 @@ function queue(worker, concurrency, payload) {
concurrency: concurrency,
payload: payload,
saturated: noop,
- unsaturated: noop,
+ unsaturated:noop,
buffer: concurrency / 4,
empty: noop,
drain: noop,
@@ -2300,6 +2295,9 @@ function queue(worker, concurrency, payload) {
unshift: function (data, callback) {
_insert(data, true, callback);
},
+ remove: function (testFn) {
+ q._tasks.remove(testFn);
+ },
process: function () {
// Avoid trying to start too many processing operations. This can occur
// when callbacks resolve synchronously (#1267).
@@ -2307,22 +2305,22 @@ function queue(worker, concurrency, payload) {
return;
}
isProcessing = true;
- while (!q.paused && numRunning < q.concurrency && q._tasks.length) {
- var tasks = [],
- data = [];
+ while(!q.paused && numRunning < q.concurrency && q._tasks.length){
+ var tasks = [], data = [];
var l = q._tasks.length;
if (q.payload) l = Math.min(l, q.payload);
for (var i = 0; i < l; i++) {
var node = q._tasks.shift();
tasks.push(node);
+ workersList.push(node);
data.push(node.data);
}
+ numRunning += 1;
+
if (q._tasks.length === 0) {
q.empty();
}
- numRunning += 1;
- workersList.push(tasks[0]);
if (numRunning === q.concurrency) {
q.saturated();
@@ -2342,16 +2340,14 @@ function queue(worker, concurrency, payload) {
workersList: function () {
return workersList;
},
- idle: function () {
+ idle: function() {
return q._tasks.length + numRunning === 0;
},
pause: function () {
q.paused = true;
},
resume: function () {
- if (q.paused === false) {
- return;
- }
+ if (q.paused === false) { return; }
q.paused = false;
setImmediate$1(q.process);
}
@@ -2437,7 +2433,7 @@ function queue(worker, concurrency, payload) {
* });
*/
function cargo(worker, payload) {
- return queue(worker, 1, payload);
+ return queue(worker, 1, payload);
}
/**
@@ -2501,13 +2497,13 @@ var eachOfSeries = doLimit(eachOfLimit, 1);
*/
function reduce(coll, memo, iteratee, callback) {
callback = once(callback || noop);
- var _iteratee = wrapAsync$1(iteratee);
- eachOfSeries(coll, function (x, i, callback) {
- _iteratee(memo, x, function (err, v) {
+ var _iteratee = wrapAsync(iteratee);
+ eachOfSeries(coll, function(x, i, callback) {
+ _iteratee(memo, x, function(err, v) {
memo = v;
callback(err);
});
- }, function (err) {
+ }, function(err) {
callback(err, memo);
});
}
@@ -2550,9 +2546,10 @@ function reduce(coll, memo, iteratee, callback) {
* });
* });
*/
-var seq$1 = rest(function seq(functions) {
- var _functions = arrayMap(functions, wrapAsync$1);
- return rest(function (args) {
+function seq(/*...functions*/) {
+ var _functions = arrayMap(arguments, wrapAsync);
+ return function(/*...args*/) {
+ var args = slice(arguments);
var that = this;
var cb = args[args.length - 1];
@@ -2562,15 +2559,17 @@ var seq$1 = rest(function seq(functions) {
cb = noop;
}
- reduce(_functions, args, function (newargs, fn, cb) {
- fn.apply(that, newargs.concat(rest(function (err, nextargs) {
+ reduce(_functions, args, function(newargs, fn, cb) {
+ fn.apply(that, newargs.concat(function(err/*, ...nextargs*/) {
+ var nextargs = slice(arguments, 1);
cb(err, nextargs);
- })));
- }, function (err, results) {
+ }));
+ },
+ function(err, results) {
cb.apply(that, [err].concat(results));
});
- });
-});
+ };
+}
/**
* Creates a function which is a composition of the passed asynchronous
@@ -2607,21 +2606,49 @@ var seq$1 = rest(function seq(functions) {
* // result now equals 15
* });
*/
-var compose = rest(function (args) {
- return seq$1.apply(null, args.reverse());
-});
+var compose = function(/*...args*/) {
+ return seq.apply(null, slice(arguments).reverse());
+};
-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);
+var _concat = Array.prototype.concat;
+
+/**
+ * The same as [`concat`]{@link module:Collections.concat} but runs a maximum of `limit` async operations at a time.
+ *
+ * @name concatLimit
+ * @static
+ * @memberOf module:Collections
+ * @method
+ * @see [async.concat]{@link module:Collections.concat}
+ * @category Collection
+ * @param {Array|Iterable|Object} coll - A collection to iterate over.
+ * @param {number} limit - The maximum number of async operations at a time.
+ * @param {AsyncFunction} iteratee - A function to apply to each item in `coll`,
+ * which should use an array as its result. Invoked with (item, callback).
+ * @param {Function} [callback] - A callback which is called after all the
+ * `iteratee` functions have finished, or an error occurs. Results is an array
+ * containing the concatenated results of the `iteratee` function. Invoked with
+ * (err, results).
+ */
+var concatLimit = function(coll, limit, iteratee, callback) {
+ callback = callback || noop;
+ var _iteratee = wrapAsync(iteratee);
+ mapLimit(coll, limit, function(val, callback) {
+ _iteratee(val, function(err /*, ...args*/) {
+ if (err) return callback(err);
+ return callback(null, slice(arguments, 1));
});
- }, function (err) {
- callback(err, result);
+ }, function(err, mapResults) {
+ var result = [];
+ for (var i = 0; i < mapResults.length; i++) {
+ if (mapResults[i]) {
+ result = _concat.apply(result, mapResults[i]);
+ }
+ }
+
+ return callback(err, result);
});
-}
+};
/**
* Applies `iteratee` to each item in `coll`, concatenating the results. Returns
@@ -2648,13 +2675,7 @@ function concat$1(eachfn, arr, fn, callback) {
* // files is now a list of filenames that exist in the 3 directories
* });
*/
-var concat = doParallel(concat$1);
-
-function doSeries(fn) {
- return function (obj, iteratee, callback) {
- return fn(eachOfSeries, obj, wrapAsync$1(iteratee), callback);
- };
-}
+var concat = doLimit(concatLimit, Infinity);
/**
* The same as [`concat`]{@link module:Collections.concat} but runs only a single async operation at a time.
@@ -2674,7 +2695,7 @@ function doSeries(fn) {
* containing the concatenated results of the `iteratee` function. Invoked with
* (err, results).
*/
-var concatSeries = doSeries(concat$1);
+var concatSeries = doLimit(concatLimit, 1);
/**
* Returns a function that when called, calls-back with the values provided.
@@ -2718,20 +2739,42 @@ var concatSeries = doSeries(concat$1);
* //...
* }, callback);
*/
-var constant = rest(function (values) {
+var constant = function(/*...values*/) {
+ var values = slice(arguments);
var args = [null].concat(values);
- return initialParams(function (ignoredArgs, callback) {
+ return function (/*...ignoredArgs, callback*/) {
+ var callback = arguments[arguments.length - 1];
return callback.apply(this, args);
- });
-});
+ };
+};
+
+/**
+ * This method returns the first argument it receives.
+ *
+ * @static
+ * @since 0.1.0
+ * @memberOf _
+ * @category Util
+ * @param {*} value Any value.
+ * @returns {*} Returns `value`.
+ * @example
+ *
+ * var object = { 'a': 1 };
+ *
+ * console.log(_.identity(object) === object);
+ * // => true
+ */
+function identity(value) {
+ return value;
+}
function _createTester(check, getResult) {
- return function (eachfn, arr, iteratee, cb) {
+ return function(eachfn, arr, iteratee, cb) {
cb = cb || noop;
var testPassed = false;
var testResult;
- eachfn(arr, function (value, _, callback) {
- iteratee(value, function (err, result) {
+ eachfn(arr, function(value, _, callback) {
+ iteratee(value, function(err, result) {
if (err) {
callback(err);
} else if (check(result) && !testResult) {
@@ -2742,7 +2785,7 @@ function _createTester(check, getResult) {
callback();
}
});
- }, function (err) {
+ }, function(err) {
if (err) {
cb(err);
} else {
@@ -2840,8 +2883,10 @@ var detectLimit = doParallelLimit(_createTester(identity, _findGetResult));
var detectSeries = doLimit(detectLimit, 1);
function consoleFunc(name) {
- return rest(function (fn, args) {
- wrapAsync$1(fn).apply(null, args.concat(rest(function (err, args) {
+ return function (fn/*, ...args*/) {
+ var args = slice(arguments, 1);
+ args.push(function (err/*, ...args*/) {
+ var args = slice(arguments, 1);
if (typeof console === 'object') {
if (err) {
if (console.error) {
@@ -2853,8 +2898,9 @@ function consoleFunc(name) {
});
}
}
- })));
- });
+ });
+ wrapAsync(fn).apply(null, args);
+ };
}
/**
@@ -2910,14 +2956,15 @@ var dir = consoleFunc('dir');
*/
function doDuring(fn, test, callback) {
callback = onlyOnce(callback || noop);
- var _fn = wrapAsync$1(fn);
- var _test = wrapAsync$1(test);
+ var _fn = wrapAsync(fn);
+ var _test = wrapAsync(test);
- var next = rest(function (err, args) {
+ function next(err/*, ...args*/) {
if (err) return callback(err);
+ var args = slice(arguments, 1);
args.push(check);
_test.apply(this, args);
- });
+ }
function check(err, truth) {
if (err) return callback(err);
@@ -2926,6 +2973,7 @@ function doDuring(fn, test, callback) {
}
check(null, true);
+
}
/**
@@ -2952,12 +3000,13 @@ function doDuring(fn, test, callback) {
*/
function doWhilst(iteratee, test, callback) {
callback = onlyOnce(callback || noop);
- var _iteratee = wrapAsync$1(iteratee);
- var next = rest(function (err, args) {
+ var _iteratee = wrapAsync(iteratee);
+ var next = function(err/*, ...args*/) {
if (err) return callback(err);
+ var args = slice(arguments, 1);
if (test.apply(this, args)) return _iteratee(next);
callback.apply(null, [null].concat(args));
- });
+ };
_iteratee(next);
}
@@ -2982,7 +3031,7 @@ function doWhilst(iteratee, test, callback) {
* callback. Invoked with (err, [results]);
*/
function doUntil(iteratee, test, callback) {
- doWhilst(iteratee, function () {
+ doWhilst(iteratee, function() {
return !test.apply(this, arguments);
}, callback);
}
@@ -3025,8 +3074,8 @@ function doUntil(iteratee, test, callback) {
*/
function during(test, fn, callback) {
callback = onlyOnce(callback || noop);
- var _fn = wrapAsync$1(fn);
- var _test = wrapAsync$1(test);
+ var _fn = wrapAsync(fn);
+ var _test = wrapAsync(test);
function next(err) {
if (err) return callback(err);
@@ -3106,7 +3155,7 @@ function _withoutIndex(iteratee) {
* });
*/
function eachLimit(coll, iteratee, callback) {
- eachOf(coll, _withoutIndex(wrapAsync$1(iteratee)), callback);
+ eachOf(coll, _withoutIndex(wrapAsync(iteratee)), callback);
}
/**
@@ -3130,7 +3179,7 @@ function eachLimit(coll, iteratee, callback) {
* `iteratee` functions have finished, or an error occurs. Invoked with (err).
*/
function eachLimit$1(coll, limit, iteratee, callback) {
- _eachOfLimit(limit)(coll, _withoutIndex(wrapAsync$1(iteratee)), callback);
+ _eachOfLimit(limit)(coll, _withoutIndex(wrapAsync(iteratee)), callback);
}
/**
@@ -3323,7 +3372,7 @@ function filterGeneric(eachfn, coll, iteratee, callback) {
callback(err);
} else {
if (v) {
- results.push({ index: index, value: x });
+ results.push({index: index, value: x});
}
callback();
}
@@ -3341,7 +3390,7 @@ function filterGeneric(eachfn, coll, iteratee, callback) {
function _filter(eachfn, coll, iteratee, callback) {
var filter = isArrayLike(coll) ? filterArray : filterGeneric;
- filter(eachfn, coll, wrapAsync$1(iteratee), callback || noop);
+ filter(eachfn, coll, wrapAsync(iteratee), callback || noop);
}
/**
@@ -3444,7 +3493,7 @@ var filterSeries = doLimit(filterLimit, 1);
*/
function forever(fn, errback) {
var done = onlyOnce(errback || noop);
- var task = wrapAsync$1(ensureAsync(fn));
+ var task = wrapAsync(ensureAsync(fn));
function next(err) {
if (err) return done(err);
@@ -3472,15 +3521,15 @@ function forever(fn, errback) {
* functions have finished, or an error occurs. Result is an `Object` whoses
* properties are arrays of values which returned the corresponding key.
*/
-var groupByLimit = function (coll, limit, iteratee, callback) {
+var groupByLimit = function(coll, limit, iteratee, callback) {
callback = callback || noop;
- var _iteratee = wrapAsync$1(iteratee);
- mapLimit(coll, limit, function (val, callback) {
- _iteratee(val, function (err, key) {
+ var _iteratee = wrapAsync(iteratee);
+ mapLimit(coll, limit, function(val, callback) {
+ _iteratee(val, function(err, key) {
if (err) return callback(err);
- return callback(null, { key: key, val: val });
+ return callback(null, {key: key, val: val});
});
- }, function (err, mapResults) {
+ }, function(err, mapResults) {
var result = {};
// from MDN, handle object having an `hasOwnProperty` prop
var hasOwnProperty = Object.prototype.hasOwnProperty;
@@ -3614,8 +3663,8 @@ var log = consoleFunc('log');
function mapValuesLimit(obj, limit, iteratee, callback) {
callback = once(callback || noop);
var newObj = {};
- var _iteratee = wrapAsync$1(iteratee);
- eachOfLimit(obj, limit, function (val, key, next) {
+ var _iteratee = wrapAsync(iteratee);
+ eachOfLimit(obj, limit, function(val, key, next) {
_iteratee(val, key, function (err, result) {
if (err) return next(err);
newObj[key] = result;
@@ -3739,25 +3788,26 @@ function memoize(fn, hasher) {
var memo = Object.create(null);
var queues = Object.create(null);
hasher = hasher || identity;
- var _fn = wrapAsync$1(fn);
+ var _fn = wrapAsync(fn);
var memoized = initialParams(function memoized(args, callback) {
var key = hasher.apply(null, args);
if (has(memo, key)) {
- setImmediate$1(function () {
+ setImmediate$1(function() {
callback.apply(null, memo[key]);
});
} else if (has(queues, key)) {
queues[key].push(callback);
} else {
queues[key] = [callback];
- _fn.apply(null, args.concat(rest(function (args) {
+ _fn.apply(null, args.concat(function(/*args*/) {
+ var args = slice(arguments);
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;
@@ -3767,7 +3817,7 @@ function memoize(fn, hasher) {
/**
* Calls `callback` on a later loop around the event loop. In Node.js this just
- * calls `setImmediate`. In the browser it will use `setImmediate` if
+ * calls `process.nextTicl`. In the browser it will use `setImmediate` if
* available, otherwise `setTimeout(callback, 0)`, which means other higher
* priority events may precede the execution of `callback`.
*
@@ -3777,7 +3827,7 @@ function memoize(fn, hasher) {
* @static
* @memberOf module:Utils
* @method
- * @alias setImmediate
+ * @see [async.setImmediate]{@link module:Utils.setImmediate}
* @category Util
* @param {Function} callback - The function to call on a later loop around
* the event loop. Invoked with (args...).
@@ -3813,13 +3863,13 @@ function _parallel(eachfn, tasks, callback) {
var results = isArrayLike(tasks) ? [] : {};
eachfn(tasks, function (task, key, callback) {
- wrapAsync$1(task)(rest(function (err, args) {
- if (args.length <= 1) {
- args = args[0];
+ wrapAsync(task)(function (err, result) {
+ if (arguments.length > 2) {
+ result = slice(arguments, 1);
}
- results[key] = args;
+ results[key] = result;
callback(err);
- }));
+ });
}, function (err) {
callback(err, results);
});
@@ -3895,7 +3945,7 @@ function _parallel(eachfn, tasks, callback) {
* });
*/
function parallelLimit(tasks, callback) {
- _parallel(eachOf, tasks, callback);
+ _parallel(eachOf, tasks, callback);
}
/**
@@ -3918,7 +3968,7 @@ function parallelLimit(tasks, callback) {
* Invoked with (err, results).
*/
function parallelLimit$1(tasks, limit, callback) {
- _parallel(_eachOfLimit(limit), tasks, callback);
+ _parallel(_eachOfLimit(limit), tasks, callback);
}
/**
@@ -3944,6 +3994,12 @@ function parallelLimit$1(tasks, limit, callback) {
* task in the list. Invoke with `queue.push(task, [callback])`,
* @property {Function} unshift - add a new task to the front of the `queue`.
* Invoke with `queue.unshift(task, [callback])`.
+ * @property {Function} remove - remove items from the queue that match a test
+ * function. The test function will be passed an object with a `data` property,
+ * and a `priority` property, if this is a
+ * [priorityQueue]{@link module:ControlFlow.priorityQueue} object.
+ * Invoked with `queue.remove(testFn)`, where `testFn` is of the form
+ * `function ({data, priority}) {}` and returns a Boolean.
* @property {Function} saturated - a callback that is called when the number of
* running workers hits the `concurrency` limit, and further tasks will be
* queued.
@@ -3965,7 +4021,8 @@ function parallelLimit$1(tasks, limit, callback) {
* @property {Function} resume - a function that resumes the processing of
* queued tasks when the queue is paused. Invoke with `queue.resume()`.
* @property {Function} kill - a function that removes the `drain` callback and
- * empties remaining tasks from the queue forcing it to go idle. Invoke with `queue.kill()`.
+ * empties remaining tasks from the queue forcing it to go idle. No more tasks
+ * should be pushed to the queue after calling this function. Invoke with `queue.kill()`.
*/
/**
@@ -4020,10 +4077,10 @@ function parallelLimit$1(tasks, limit, callback) {
* });
*/
var queue$1 = function (worker, concurrency) {
- var _worker = wrapAsync$1(worker);
- return queue(function (items, cb) {
- _worker(items[0], cb);
- }, concurrency, 1);
+ var _worker = wrapAsync(worker);
+ return queue(function (items, cb) {
+ _worker(items[0], cb);
+ }, concurrency, 1);
};
/**
@@ -4049,12 +4106,12 @@ var queue$1 = function (worker, concurrency) {
* array of `tasks` is given, all tasks will be assigned the same priority.
* * The `unshift` method was removed.
*/
-var priorityQueue = function (worker, concurrency) {
+var priorityQueue = function(worker, concurrency) {
// Start with a normal queue
var q = queue$1(worker, concurrency);
// Override push to accept second parameter representing priority
- q.push = function (data, priority, callback) {
+ q.push = function(data, priority, callback) {
if (callback == null) callback = noop;
if (typeof callback !== 'function') {
throw new Error('task callback must be a function');
@@ -4065,7 +4122,7 @@ var priorityQueue = function (worker, concurrency) {
}
if (data.length === 0) {
// call drain immediately if there are no tasks
- return setImmediate$1(function () {
+ return setImmediate$1(function() {
q.drain();
});
}
@@ -4139,12 +4196,10 @@ function race(tasks, callback) {
if (!isArray(tasks)) return callback(new TypeError('First argument to race must be an array of functions'));
if (!tasks.length) return callback();
for (var i = 0, l = tasks.length; i < l; i++) {
- wrapAsync$1(tasks[i])(callback);
+ wrapAsync(tasks[i])(callback);
}
}
-var slice = Array.prototype.slice;
-
/**
* Same as [`reduce`]{@link module:Collections.reduce}, only operates on `array` in reverse order.
*
@@ -4167,9 +4222,9 @@ var slice = Array.prototype.slice;
* `iteratee` functions have finished. Result is the reduced value. Invoked with
* (err, result).
*/
-function reduceRight(array, memo, iteratee, callback) {
- var reversed = slice.call(array).reverse();
- reduce(reversed, memo, iteratee, callback);
+function reduceRight (array, memo, iteratee, callback) {
+ var reversed = slice(array).reverse();
+ reduce(reversed, memo, iteratee, callback);
}
/**
@@ -4212,67 +4267,26 @@ function reduceRight(array, memo, iteratee, callback) {
* });
*/
function reflect(fn) {
- var _fn = wrapAsync$1(fn);
+ var _fn = wrapAsync(fn);
return initialParams(function reflectOn(args, reflectCallback) {
- args.push(rest(function callback(err, cbArgs) {
- if (err) {
- reflectCallback(null, {
- error: err
- });
+ args.push(function callback(error, cbArg) {
+ if (error) {
+ reflectCallback(null, { error: error });
} else {
- var value = null;
- if (cbArgs.length === 1) {
- value = cbArgs[0];
- } else if (cbArgs.length > 1) {
- value = cbArgs;
+ var value;
+ if (arguments.length <= 2) {
+ value = cbArg;
+ } else {
+ value = slice(arguments, 1);
}
- reflectCallback(null, {
- value: value
- });
+ reflectCallback(null, { value: value });
}
- }));
+ });
return _fn.apply(this, args);
});
}
-function reject$1(eachfn, arr, iteratee, callback) {
- _filter(eachfn, arr, function (value, cb) {
- iteratee(value, function (err, v) {
- cb(err, !v);
- });
- }, callback);
-}
-
-/**
- * The opposite of [`filter`]{@link module:Collections.filter}. Removes values that pass an `async` truth test.
- *
- * @name reject
- * @static
- * @memberOf module:Collections
- * @method
- * @see [async.filter]{@link module:Collections.filter}
- * @category Collection
- * @param {Array|Iterable|Object} coll - A collection to iterate over.
- * @param {Function} iteratee - An async truth test to apply to each item in
- * `coll`.
- * The should complete with a boolean value as its `result`.
- * Invoked with (item, callback).
- * @param {Function} [callback] - A callback which is called after all the
- * `iteratee` functions have finished. Invoked with (err, results).
- * @example
- *
- * async.reject(['file1','file2','file3'], function(filePath, callback) {
- * fs.access(filePath, function(err) {
- * callback(null, !err)
- * });
- * }, function(err, results) {
- * // results now equals an array of missing files
- * createFiles(results);
- * });
- */
-var reject = doParallel(reject$1);
-
/**
* A helper function that wraps an array or an object of functions with `reflect`.
*
@@ -4346,13 +4360,50 @@ function reflectAll(tasks) {
results = arrayMap(tasks, reflect);
} else {
results = {};
- baseForOwn(tasks, function (task, key) {
+ baseForOwn(tasks, function(task, key) {
results[key] = reflect.call(this, task);
});
}
return results;
}
+function reject$1(eachfn, arr, iteratee, callback) {
+ _filter(eachfn, arr, function(value, cb) {
+ iteratee(value, function(err, v) {
+ cb(err, !v);
+ });
+ }, callback);
+}
+
+/**
+ * The opposite of [`filter`]{@link module:Collections.filter}. Removes values that pass an `async` truth test.
+ *
+ * @name reject
+ * @static
+ * @memberOf module:Collections
+ * @method
+ * @see [async.filter]{@link module:Collections.filter}
+ * @category Collection
+ * @param {Array|Iterable|Object} coll - A collection to iterate over.
+ * @param {Function} iteratee - An async truth test to apply to each item in
+ * `coll`.
+ * The should complete with a boolean value as its `result`.
+ * Invoked with (item, callback).
+ * @param {Function} [callback] - A callback which is called after all the
+ * `iteratee` functions have finished. Invoked with (err, results).
+ * @example
+ *
+ * async.reject(['file1','file2','file3'], function(filePath, callback) {
+ * fs.access(filePath, function(err) {
+ * callback(null, !err)
+ * });
+ * }, function(err, results) {
+ * // results now equals an array of missing files
+ * createFiles(results);
+ * });
+ */
+var reject = doParallel(reject$1);
+
/**
* The same as [`reject`]{@link module:Collections.reject} but runs a maximum of `limit` async operations at a
* time.
@@ -4492,8 +4543,8 @@ function constant$1(value) {
* // 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:
+ * // to retry individual methods that are not as reliable within other
+ * // control flow functions, use the `retryable` wrapper:
* async.auto({
* users: api.getUsers.bind(api),
* payments: async.retryable(3, api.getPayments.bind(api))
@@ -4515,7 +4566,9 @@ function retry(opts, task, callback) {
if (typeof t === 'object') {
acc.times = +t.times || DEFAULT_TIMES;
- acc.intervalFunc = typeof t.interval === 'function' ? t.interval : constant$1(+t.interval || DEFAULT_INTERVAL);
+ acc.intervalFunc = typeof t.interval === 'function' ?
+ t.interval :
+ constant$1(+t.interval || DEFAULT_INTERVAL);
acc.errorFilter = t.errorFilter;
} else if (typeof t === 'number' || typeof t === 'string') {
@@ -4537,12 +4590,14 @@ function retry(opts, task, callback) {
throw new Error("Invalid arguments for async.retry");
}
- var _task = wrapAsync$1(task);
+ var _task = wrapAsync(task);
var attempt = 1;
function retryAttempt() {
- _task(function (err) {
- if (err && attempt++ < options.times && (typeof options.errorFilter != 'function' || options.errorFilter(err))) {
+ _task(function(err) {
+ if (err && attempt++ < options.times &&
+ (typeof options.errorFilter != 'function' ||
+ options.errorFilter(err))) {
setTimeout(retryAttempt, options.intervalFunc(attempt));
} else {
callback.apply(null, arguments);
@@ -4586,13 +4641,15 @@ var retryable = function (opts, task) {
task = opts;
opts = null;
}
- var _task = wrapAsync$1(task);
+ var _task = wrapAsync(task);
return initialParams(function (args, callback) {
function taskFn(cb) {
_task.apply(null, args.concat(cb));
}
- if (opts) retry(opts, taskFn, callback);else retry(taskFn, callback);
+ if (opts) retry(opts, taskFn, callback);
+ else retry(taskFn, callback);
+
});
};
@@ -4661,7 +4718,7 @@ var retryable = function (opts, task) {
* });
*/
function series(tasks, callback) {
- _parallel(eachOfSeries, tasks, callback);
+ _parallel(eachOfSeries, tasks, callback);
}
/**
@@ -4788,12 +4845,12 @@ var someSeries = doLimit(someLimit, 1);
* // result callback
* });
*/
-function sortBy(coll, iteratee, callback) {
- var _iteratee = wrapAsync$1(iteratee);
+function sortBy (coll, iteratee, callback) {
+ var _iteratee = wrapAsync(iteratee);
map(coll, function (x, callback) {
_iteratee(x, function (err, criteria) {
if (err) return callback(err);
- callback(null, { value: x, criteria: criteria });
+ callback(null, {value: x, criteria: criteria});
});
}, function (err, results) {
if (err) return callback(err);
@@ -4801,8 +4858,7 @@ function sortBy(coll, iteratee, callback) {
});
function comparator(left, right) {
- var a = left.criteria,
- b = right.criteria;
+ var a = left.criteria, b = right.criteria;
return a < b ? -1 : a > b ? 1 : 0;
}
}
@@ -4849,40 +4905,39 @@ function sortBy(coll, iteratee, callback) {
* });
*/
function timeout(asyncFn, milliseconds, info) {
- var originalCallback, timer;
- var timedOut = false;
+ var fn = wrapAsync(asyncFn);
- function injectedCallback() {
- if (!timedOut) {
- originalCallback.apply(null, arguments);
- clearTimeout(timer);
- }
- }
-
- function timeoutCallback() {
- var name = asyncFn.name || 'anonymous';
- var error = new Error('Callback function "' + name + '" timed out.');
- error.code = 'ETIMEDOUT';
- if (info) {
- error.info = info;
+ return initialParams(function (args, callback) {
+ var timedOut = false;
+ var timer;
+
+ function timeoutCallback() {
+ var name = asyncFn.name || 'anonymous';
+ var error = new Error('Callback function "' + name + '" timed out.');
+ error.code = 'ETIMEDOUT';
+ if (info) {
+ error.info = info;
+ }
+ timedOut = true;
+ callback(error);
}
- timedOut = true;
- originalCallback(error);
- }
- var fn = wrapAsync$1(asyncFn);
+ args.push(function () {
+ if (!timedOut) {
+ callback.apply(null, arguments);
+ clearTimeout(timer);
+ }
+ });
- return initialParams(function (args, origCallback) {
- originalCallback = origCallback;
// setup timer and call original function
timer = setTimeout(timeoutCallback, milliseconds);
- fn.apply(null, args.concat(injectedCallback));
+ fn.apply(null, args);
});
}
/* Built-in method references for those with the same name as other `lodash` methods. */
var nativeCeil = Math.ceil;
-var nativeMax$1 = Math.max;
+var nativeMax = Math.max;
/**
* The base implementation of `_.range` and `_.rangeRight` which doesn't
@@ -4897,7 +4952,7 @@ var nativeMax$1 = Math.max;
*/
function baseRange(start, end, step, fromRight) {
var index = -1,
- length = nativeMax$1(nativeCeil((end - start) / (step || 1)), 0),
+ length = nativeMax(nativeCeil((end - start) / (step || 1)), 0),
result = Array(length);
while (length--) {
@@ -4924,8 +4979,8 @@ function baseRange(start, end, step, fromRight) {
* @param {Function} callback - see [async.map]{@link module:Collections.map}.
*/
function timeLimit(count, limit, iteratee, callback) {
- var _iteratee = wrapAsync$1(iteratee);
- mapLimit(baseRange(0, count, 1), limit, _iteratee, callback);
+ var _iteratee = wrapAsync(iteratee);
+ mapLimit(baseRange(0, count, 1), limit, _iteratee, callback);
}
/**
@@ -5020,23 +5075,79 @@ var timesSeries = doLimit(timeLimit, 1);
* // result is equal to {a: 2, b: 4, c: 6}
* })
*/
-function transform(coll, accumulator, iteratee, callback) {
+function transform (coll, accumulator, iteratee, callback) {
if (arguments.length <= 3) {
callback = iteratee;
iteratee = accumulator;
accumulator = isArray(coll) ? [] : {};
}
callback = once(callback || noop);
- var _iteratee = wrapAsync$1(iteratee);
+ var _iteratee = wrapAsync(iteratee);
- eachOf(coll, function (v, k, cb) {
+ eachOf(coll, function(v, k, cb) {
_iteratee(accumulator, v, k, cb);
- }, function (err) {
+ }, function(err) {
callback(err, accumulator);
});
}
/**
+ * It runs each task in series but stops whenever any of the functions were
+ * successful. If one of the tasks were successful, the `callback` will be
+ * passed the result of the successful task. If all tasks fail, the callback
+ * will be passed the error and result (if any) of the final attempt.
+ *
+ * @name tryEach
+ * @static
+ * @memberOf module:ControlFlow
+ * @method
+ * @category Control Flow
+ * @param {Array|Iterable|Object} tasks - A collection 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.
+ * @param {Function} [callback] - An optional callback which is called when one
+ * of the tasks has succeeded, or all have failed. It receives the `err` and
+ * `result` arguments of the last attempt at completing the `task`. Invoked with
+ * (err, results).
+ * @example
+ * async.tryEach([
+ * function getDataFromFirstWebsite(callback) {
+ * // Try getting the data from the first website
+ * callback(err, data);
+ * },
+ * function getDataFromSecondWebsite(callback) {
+ * // First website failed,
+ * // Try getting the data from the backup website
+ * callback(err, data);
+ * }
+ * ],
+ * // optional callback
+ * function(err, results) {
+ * Now do something with the data.
+ * });
+ *
+ */
+function tryEach(tasks, callback) {
+ var error = null;
+ var result;
+ callback = callback || noop;
+ eachSeries(tasks, function(task, callback) {
+ wrapAsync(task)(function (err, res/*, ...args*/) {
+ if (arguments.length > 2) {
+ result = slice(arguments, 1);
+ } else {
+ result = res;
+ }
+ error = err;
+ callback(!err);
+ });
+ }, function () {
+ callback(error, result);
+ });
+}
+
+/**
* Undoes a [memoize]{@link module:Utils.memoize}d function, reverting it to the original,
* unmemoized form. Handy for testing.
*
@@ -5091,13 +5202,14 @@ function unmemoize(fn) {
*/
function whilst(test, iteratee, callback) {
callback = onlyOnce(callback || noop);
- var _iteratee = wrapAsync$1(iteratee);
+ var _iteratee = wrapAsync(iteratee);
if (!test()) return callback(null);
- var next = rest(function (err, args) {
+ var next = function(err/*, ...args*/) {
if (err) return callback(err);
if (test()) return _iteratee(next);
+ var args = slice(arguments, 1);
callback.apply(null, [null].concat(args));
- });
+ };
_iteratee(next);
}
@@ -5124,7 +5236,7 @@ function whilst(test, iteratee, callback) {
* callback. Invoked with (err, [results]);
*/
function until(test, iteratee, callback) {
- whilst(function () {
+ whilst(function() {
return !test.apply(this, arguments);
}, iteratee, callback);
}
@@ -5186,30 +5298,25 @@ function until(test, iteratee, callback) {
* callback(null, 'done');
* }
*/
-var waterfall = function (tasks, callback) {
+var waterfall = function(tasks, callback) {
callback = once(callback || noop);
if (!isArray(tasks)) return callback(new Error('First argument to waterfall must be an array of functions'));
if (!tasks.length) return callback();
var taskIndex = 0;
function nextTask(args) {
- if (taskIndex === tasks.length) {
- return callback.apply(null, [null].concat(args));
- }
-
- var taskCallback = onlyOnce(rest(function (err, args) {
- if (err) {
- return callback.apply(null, [err].concat(args));
- }
- nextTask(args);
- }));
-
- args.push(taskCallback);
-
- var task = wrapAsync$1(tasks[taskIndex++]);
+ var task = wrapAsync(tasks[taskIndex++]);
+ args.push(onlyOnce(next));
task.apply(null, args);
}
+ function next(err/*, ...args*/) {
+ if (err || taskIndex === tasks.length) {
+ return callback.apply(null, arguments);
+ }
+ nextTask(slice(arguments, 1));
+ }
+
nextTask([]);
};
@@ -5261,6 +5368,7 @@ var waterfall = function (tasks, callback) {
* @see AsyncFunction
*/
+
/**
* A collection of `async` functions for manipulating collections, such as
* arrays and objects.
@@ -5278,110 +5386,120 @@ var waterfall = function (tasks, callback) {
*/
var index = {
- applyEach: applyEach,
- applyEachSeries: applyEachSeries,
- apply: apply$2,
- asyncify: asyncify,
- auto: auto,
- autoInject: autoInject,
- 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: eachLimit,
- eachLimit: eachLimit$1,
- eachOf: eachOf,
- eachOfLimit: eachOfLimit,
- eachOfSeries: eachOfSeries,
- eachSeries: eachSeries,
- ensureAsync: ensureAsync,
- every: every,
- everyLimit: everyLimit,
- everySeries: everySeries,
- filter: filter,
- filterLimit: filterLimit,
- filterSeries: filterSeries,
- forever: forever,
- groupBy: groupBy,
- groupByLimit: groupByLimit,
- groupBySeries: groupBySeries,
- log: log,
- map: map,
- mapLimit: mapLimit,
- mapSeries: mapSeries,
- mapValues: mapValues,
- mapValuesLimit: mapValuesLimit,
- mapValuesSeries: mapValuesSeries,
- memoize: memoize,
- nextTick: nextTick,
- parallel: parallelLimit,
- parallelLimit: parallelLimit$1,
- priorityQueue: priorityQueue,
- queue: queue$1,
- race: race,
- reduce: reduce,
- reduceRight: reduceRight,
- reflect: reflect,
- reflectAll: reflectAll,
- reject: reject,
- rejectLimit: rejectLimit,
- rejectSeries: rejectSeries,
- retry: retry,
- retryable: retryable,
- seq: seq$1,
- series: series,
- setImmediate: setImmediate$1,
- some: some,
- someLimit: someLimit,
- someSeries: someSeries,
- sortBy: sortBy,
- timeout: timeout,
- times: times,
- timesLimit: timeLimit,
- timesSeries: timesSeries,
- transform: transform,
- unmemoize: unmemoize,
- until: until,
- waterfall: waterfall,
- whilst: whilst,
-
- // aliases
- all: every,
- any: some,
- forEach: eachLimit,
- forEachSeries: eachSeries,
- forEachLimit: eachLimit$1,
- forEachOf: eachOf,
- forEachOfSeries: eachOfSeries,
- forEachOfLimit: eachOfLimit,
- inject: reduce,
- foldl: reduce,
- foldr: reduceRight,
- select: filter,
- selectLimit: filterLimit,
- selectSeries: filterSeries,
- wrapSync: asyncify
+ apply: apply,
+ applyEach: applyEach,
+ applyEachSeries: applyEachSeries,
+ asyncify: asyncify,
+ auto: auto,
+ autoInject: autoInject,
+ cargo: cargo,
+ compose: compose,
+ concat: concat,
+ concatLimit: concatLimit,
+ concatSeries: concatSeries,
+ constant: constant,
+ detect: detect,
+ detectLimit: detectLimit,
+ detectSeries: detectSeries,
+ dir: dir,
+ doDuring: doDuring,
+ doUntil: doUntil,
+ doWhilst: doWhilst,
+ during: during,
+ each: eachLimit,
+ eachLimit: eachLimit$1,
+ eachOf: eachOf,
+ eachOfLimit: eachOfLimit,
+ eachOfSeries: eachOfSeries,
+ eachSeries: eachSeries,
+ ensureAsync: ensureAsync,
+ every: every,
+ everyLimit: everyLimit,
+ everySeries: everySeries,
+ filter: filter,
+ filterLimit: filterLimit,
+ filterSeries: filterSeries,
+ forever: forever,
+ groupBy: groupBy,
+ groupByLimit: groupByLimit,
+ groupBySeries: groupBySeries,
+ log: log,
+ map: map,
+ mapLimit: mapLimit,
+ mapSeries: mapSeries,
+ mapValues: mapValues,
+ mapValuesLimit: mapValuesLimit,
+ mapValuesSeries: mapValuesSeries,
+ memoize: memoize,
+ nextTick: nextTick,
+ parallel: parallelLimit,
+ parallelLimit: parallelLimit$1,
+ priorityQueue: priorityQueue,
+ queue: queue$1,
+ race: race,
+ reduce: reduce,
+ reduceRight: reduceRight,
+ reflect: reflect,
+ reflectAll: reflectAll,
+ reject: reject,
+ rejectLimit: rejectLimit,
+ rejectSeries: rejectSeries,
+ retry: retry,
+ retryable: retryable,
+ seq: seq,
+ series: series,
+ setImmediate: setImmediate$1,
+ some: some,
+ someLimit: someLimit,
+ someSeries: someSeries,
+ sortBy: sortBy,
+ timeout: timeout,
+ times: times,
+ timesLimit: timeLimit,
+ timesSeries: timesSeries,
+ transform: transform,
+ tryEach: tryEach,
+ unmemoize: unmemoize,
+ until: until,
+ waterfall: waterfall,
+ whilst: whilst,
+
+ // aliases
+ all: every,
+ allLimit: everyLimit,
+ allSeries: everySeries,
+ any: some,
+ anyLimit: someLimit,
+ anySeries: someSeries,
+ find: detect,
+ findLimit: detectLimit,
+ findSeries: detectSeries,
+ forEach: eachLimit,
+ forEachSeries: eachSeries,
+ forEachLimit: eachLimit$1,
+ forEachOf: eachOf,
+ forEachOfSeries: eachOfSeries,
+ forEachOfLimit: eachOfLimit,
+ inject: reduce,
+ foldl: reduce,
+ foldr: reduceRight,
+ select: filter,
+ selectLimit: filterLimit,
+ selectSeries: filterSeries,
+ wrapSync: asyncify
};
exports['default'] = index;
+exports.apply = apply;
exports.applyEach = applyEach;
exports.applyEachSeries = applyEachSeries;
-exports.apply = apply$2;
exports.asyncify = asyncify;
exports.auto = auto;
exports.autoInject = autoInject;
exports.cargo = cargo;
exports.compose = compose;
exports.concat = concat;
+exports.concatLimit = concatLimit;
exports.concatSeries = concatSeries;
exports.constant = constant;
exports.detect = detect;
@@ -5432,7 +5550,7 @@ exports.rejectLimit = rejectLimit;
exports.rejectSeries = rejectSeries;
exports.retry = retry;
exports.retryable = retryable;
-exports.seq = seq$1;
+exports.seq = seq;
exports.series = series;
exports.setImmediate = setImmediate$1;
exports.some = some;
@@ -5444,6 +5562,7 @@ exports.times = times;
exports.timesLimit = timeLimit;
exports.timesSeries = timesSeries;
exports.transform = transform;
+exports.tryEach = tryEach;
exports.unmemoize = unmemoize;
exports.until = until;
exports.waterfall = waterfall;