1 | (function (global, factory) {
|
2 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
|
3 | typeof define === 'function' && define.amd ? define(['exports'], factory) :
|
4 | (factory((global.async = global.async || {})));
|
5 | }(this, function (exports) { 'use strict';
|
6 |
|
7 | /**
|
8 | * A faster alternative to `Function#apply`, this function invokes `func`
|
9 | * with the `this` binding of `thisArg` and the arguments of `args`.
|
10 | *
|
11 | * @private
|
12 | * @param {Function} func The function to invoke.
|
13 | * @param {*} thisArg The `this` binding of `func`.
|
14 | * @param {Array} args The arguments to invoke `func` with.
|
15 | * @returns {*} Returns the result of `func`.
|
16 | */
|
17 | function apply(func, thisArg, args) {
|
18 | var length = args.length;
|
19 | switch (length) {
|
20 | case 0: return func.call(thisArg);
|
21 | case 1: return func.call(thisArg, args[0]);
|
22 | case 2: return func.call(thisArg, args[0], args[1]);
|
23 | case 3: return func.call(thisArg, args[0], args[1], args[2]);
|
24 | }
|
25 | return func.apply(thisArg, args);
|
26 | }
|
27 |
|
28 | /**
|
29 | * Checks if `value` is the
|
30 | * [language type](http://www.ecma-international.org/ecma-262/6.0/#sec-ecmascript-language-types)
|
31 | * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
|
32 | *
|
33 | * @static
|
34 | * @memberOf _
|
35 | * @since 0.1.0
|
36 | * @category Lang
|
37 | * @param {*} value The value to check.
|
38 | * @returns {boolean} Returns `true` if `value` is an object, else `false`.
|
39 | * @example
|
40 | *
|
41 | * _.isObject({});
|
42 | * // => true
|
43 | *
|
44 | * _.isObject([1, 2, 3]);
|
45 | * // => true
|
46 | *
|
47 | * _.isObject(_.noop);
|
48 | * // => true
|
49 | *
|
50 | * _.isObject(null);
|
51 | * // => false
|
52 | */
|
53 | function isObject(value) {
|
54 | var type = typeof value;
|
55 | return !!value && (type == 'object' || type == 'function');
|
56 | }
|
57 |
|
58 | var funcTag = '[object Function]';
|
59 | var genTag = '[object GeneratorFunction]';
|
60 | /** Used for built-in method references. */
|
61 | var objectProto = Object.prototype;
|
62 |
|
63 | /**
|
64 | * Used to resolve the
|
65 | * [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
|
66 | * of values.
|
67 | */
|
68 | var objectToString = objectProto.toString;
|
69 |
|
70 | /**
|
71 | * Checks if `value` is classified as a `Function` object.
|
72 | *
|
73 | * @static
|
74 | * @memberOf _
|
75 | * @since 0.1.0
|
76 | * @category Lang
|
77 | * @param {*} value The value to check.
|
78 | * @returns {boolean} Returns `true` if `value` is correctly classified,
|
79 | * else `false`.
|
80 | * @example
|
81 | *
|
82 | * _.isFunction(_);
|
83 | * // => true
|
84 | *
|
85 | * _.isFunction(/abc/);
|
86 | * // => false
|
87 | */
|
88 | function isFunction(value) {
|
89 | // The use of `Object#toString` avoids issues with the `typeof` operator
|
90 | // in Safari 8 which returns 'object' for typed array and weak map constructors,
|
91 | // and PhantomJS 1.9 which returns 'function' for `NodeList` instances.
|
92 | var tag = isObject(value) ? objectToString.call(value) : '';
|
93 | return tag == funcTag || tag == genTag;
|
94 | }
|
95 |
|
96 | /**
|
97 | * Checks if `value` is object-like. A value is object-like if it's not `null`
|
98 | * and has a `typeof` result of "object".
|
99 | *
|
100 | * @static
|
101 | * @memberOf _
|
102 | * @since 4.0.0
|
103 | * @category Lang
|
104 | * @param {*} value The value to check.
|
105 | * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
|
106 | * @example
|
107 | *
|
108 | * _.isObjectLike({});
|
109 | * // => true
|
110 | *
|
111 | * _.isObjectLike([1, 2, 3]);
|
112 | * // => true
|
113 | *
|
114 | * _.isObjectLike(_.noop);
|
115 | * // => false
|
116 | *
|
117 | * _.isObjectLike(null);
|
118 | * // => false
|
119 | */
|
120 | function isObjectLike(value) {
|
121 | return !!value && typeof value == 'object';
|
122 | }
|
123 |
|
124 | /** `Object#toString` result references. */
|
125 | var symbolTag = '[object Symbol]';
|
126 |
|
127 | /** Used for built-in method references. */
|
128 | var objectProto$1 = Object.prototype;
|
129 |
|
130 | /**
|
131 | * Used to resolve the
|
132 | * [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
|
133 | * of values.
|
134 | */
|
135 | var objectToString$1 = objectProto$1.toString;
|
136 |
|
137 | /**
|
138 | * Checks if `value` is classified as a `Symbol` primitive or object.
|
139 | *
|
140 | * @static
|
141 | * @memberOf _
|
142 | * @since 4.0.0
|
143 | * @category Lang
|
144 | * @param {*} value The value to check.
|
145 | * @returns {boolean} Returns `true` if `value` is correctly classified,
|
146 | * else `false`.
|
147 | * @example
|
148 | *
|
149 | * _.isSymbol(Symbol.iterator);
|
150 | * // => true
|
151 | *
|
152 | * _.isSymbol('abc');
|
153 | * // => false
|
154 | */
|
155 | function isSymbol(value) {
|
156 | return typeof value == 'symbol' ||
|
157 | (isObjectLike(value) && objectToString$1.call(value) == symbolTag);
|
158 | }
|
159 |
|
160 | /** Used as references for various `Number` constants. */
|
161 | var NAN = 0 / 0;
|
162 |
|
163 | /** Used to match leading and trailing whitespace. */
|
164 | var reTrim = /^\s+|\s+$/g;
|
165 |
|
166 | /** Used to detect bad signed hexadecimal string values. */
|
167 | var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
|
168 |
|
169 | /** Used to detect binary string values. */
|
170 | var reIsBinary = /^0b[01]+$/i;
|
171 |
|
172 | /** Used to detect octal string values. */
|
173 | var reIsOctal = /^0o[0-7]+$/i;
|
174 |
|
175 | /** Built-in method references without a dependency on `root`. */
|
176 | var freeParseInt = parseInt;
|
177 |
|
178 | /**
|
179 | * Converts `value` to a number.
|
180 | *
|
181 | * @static
|
182 | * @memberOf _
|
183 | * @since 4.0.0
|
184 | * @category Lang
|
185 | * @param {*} value The value to process.
|
186 | * @returns {number} Returns the number.
|
187 | * @example
|
188 | *
|
189 | * _.toNumber(3.2);
|
190 | * // => 3.2
|
191 | *
|
192 | * _.toNumber(Number.MIN_VALUE);
|
193 | * // => 5e-324
|
194 | *
|
195 | * _.toNumber(Infinity);
|
196 | * // => Infinity
|
197 | *
|
198 | * _.toNumber('3.2');
|
199 | * // => 3.2
|
200 | */
|
201 | function toNumber(value) {
|
202 | if (typeof value == 'number') {
|
203 | return value;
|
204 | }
|
205 | if (isSymbol(value)) {
|
206 | return NAN;
|
207 | }
|
208 | if (isObject(value)) {
|
209 | var other = isFunction(value.valueOf) ? value.valueOf() : value;
|
210 | value = isObject(other) ? (other + '') : other;
|
211 | }
|
212 | if (typeof value != 'string') {
|
213 | return value === 0 ? value : +value;
|
214 | }
|
215 | value = value.replace(reTrim, '');
|
216 | var isBinary = reIsBinary.test(value);
|
217 | return (isBinary || reIsOctal.test(value))
|
218 | ? freeParseInt(value.slice(2), isBinary ? 2 : 8)
|
219 | : (reIsBadHex.test(value) ? NAN : +value);
|
220 | }
|
221 |
|
222 | var INFINITY = 1 / 0;
|
223 | var MAX_INTEGER = 1.7976931348623157e+308;
|
224 | /**
|
225 | * Converts `value` to a finite number.
|
226 | *
|
227 | * @static
|
228 | * @memberOf _
|
229 | * @since 4.12.0
|
230 | * @category Lang
|
231 | * @param {*} value The value to convert.
|
232 | * @returns {number} Returns the converted number.
|
233 | * @example
|
234 | *
|
235 | * _.toFinite(3.2);
|
236 | * // => 3.2
|
237 | *
|
238 | * _.toFinite(Number.MIN_VALUE);
|
239 | * // => 5e-324
|
240 | *
|
241 | * _.toFinite(Infinity);
|
242 | * // => 1.7976931348623157e+308
|
243 | *
|
244 | * _.toFinite('3.2');
|
245 | * // => 3.2
|
246 | */
|
247 | function toFinite(value) {
|
248 | if (!value) {
|
249 | return value === 0 ? value : 0;
|
250 | }
|
251 | value = toNumber(value);
|
252 | if (value === INFINITY || value === -INFINITY) {
|
253 | var sign = (value < 0 ? -1 : 1);
|
254 | return sign * MAX_INTEGER;
|
255 | }
|
256 | return value === value ? value : 0;
|
257 | }
|
258 |
|
259 | /**
|
260 | * Converts `value` to an integer.
|
261 | *
|
262 | * **Note:** This method is loosely based on
|
263 | * [`ToInteger`](http://www.ecma-international.org/ecma-262/6.0/#sec-tointeger).
|
264 | *
|
265 | * @static
|
266 | * @memberOf _
|
267 | * @since 4.0.0
|
268 | * @category Lang
|
269 | * @param {*} value The value to convert.
|
270 | * @returns {number} Returns the converted integer.
|
271 | * @example
|
272 | *
|
273 | * _.toInteger(3.2);
|
274 | * // => 3
|
275 | *
|
276 | * _.toInteger(Number.MIN_VALUE);
|
277 | * // => 0
|
278 | *
|
279 | * _.toInteger(Infinity);
|
280 | * // => 1.7976931348623157e+308
|
281 | *
|
282 | * _.toInteger('3.2');
|
283 | * // => 3
|
284 | */
|
285 | function toInteger(value) {
|
286 | var result = toFinite(value),
|
287 | remainder = result % 1;
|
288 |
|
289 | return result === result ? (remainder ? result - remainder : result) : 0;
|
290 | }
|
291 |
|
292 | /** Used as the `TypeError` message for "Functions" methods. */
|
293 | var FUNC_ERROR_TEXT = 'Expected a function';
|
294 |
|
295 | /* Built-in method references for those with the same name as other `lodash` methods. */
|
296 | var nativeMax = Math.max;
|
297 |
|
298 | /**
|
299 | * Creates a function that invokes `func` with the `this` binding of the
|
300 | * created function and arguments from `start` and beyond provided as
|
301 | * an array.
|
302 | *
|
303 | * **Note:** This method is based on the
|
304 | * [rest parameter](https://mdn.io/rest_parameters).
|
305 | *
|
306 | * @static
|
307 | * @memberOf _
|
308 | * @since 4.0.0
|
309 | * @category Function
|
310 | * @param {Function} func The function to apply a rest parameter to.
|
311 | * @param {number} [start=func.length-1] The start position of the rest parameter.
|
312 | * @returns {Function} Returns the new function.
|
313 | * @example
|
314 | *
|
315 | * var say = _.rest(function(what, names) {
|
316 | * return what + ' ' + _.initial(names).join(', ') +
|
317 | * (_.size(names) > 1 ? ', & ' : '') + _.last(names);
|
318 | * });
|
319 | *
|
320 | * say('hello', 'fred', 'barney', 'pebbles');
|
321 | * // => 'hello fred, barney, & pebbles'
|
322 | */
|
323 | function rest(func, start) {
|
324 | if (typeof func != 'function') {
|
325 | throw new TypeError(FUNC_ERROR_TEXT);
|
326 | }
|
327 | start = nativeMax(start === undefined ? (func.length - 1) : toInteger(start), 0);
|
328 | return function() {
|
329 | var args = arguments,
|
330 | index = -1,
|
331 | length = nativeMax(args.length - start, 0),
|
332 | array = Array(length);
|
333 |
|
334 | while (++index < length) {
|
335 | array[index] = args[start + index];
|
336 | }
|
337 | switch (start) {
|
338 | case 0: return func.call(this, array);
|
339 | case 1: return func.call(this, args[0], array);
|
340 | case 2: return func.call(this, args[0], args[1], array);
|
341 | }
|
342 | var otherArgs = Array(start + 1);
|
343 | index = -1;
|
344 | while (++index < start) {
|
345 | otherArgs[index] = args[index];
|
346 | }
|
347 | otherArgs[start] = array;
|
348 | return apply(func, this, otherArgs);
|
349 | };
|
350 | }
|
351 |
|
352 | function initialParams (fn) {
|
353 | return rest(function (args /*..., callback*/) {
|
354 | var callback = args.pop();
|
355 | fn.call(this, args, callback);
|
356 | });
|
357 | }
|
358 |
|
359 | function applyEach$1(eachfn) {
|
360 | return rest(function (fns, args) {
|
361 | var go = initialParams(function (args, callback) {
|
362 | var that = this;
|
363 | return eachfn(fns, function (fn, cb) {
|
364 | fn.apply(that, args.concat([cb]));
|
365 | }, callback);
|
366 | });
|
367 | if (args.length) {
|
368 | return go.apply(this, args);
|
369 | } else {
|
370 | return go;
|
371 | }
|
372 | });
|
373 | }
|
374 |
|
375 | /**
|
376 | * The base implementation of `_.property` without support for deep paths.
|
377 | *
|
378 | * @private
|
379 | * @param {string} key The key of the property to get.
|
380 | * @returns {Function} Returns the new accessor function.
|
381 | */
|
382 | function baseProperty(key) {
|
383 | return function(object) {
|
384 | return object == null ? undefined : object[key];
|
385 | };
|
386 | }
|
387 |
|
388 | /**
|
389 | * Gets the "length" property value of `object`.
|
390 | *
|
391 | * **Note:** This function is used to avoid a
|
392 | * [JIT bug](https://bugs.webkit.org/show_bug.cgi?id=142792) that affects
|
393 | * Safari on at least iOS 8.1-8.3 ARM64.
|
394 | *
|
395 | * @private
|
396 | * @param {Object} object The object to query.
|
397 | * @returns {*} Returns the "length" value.
|
398 | */
|
399 | var getLength = baseProperty('length');
|
400 |
|
401 | /** Used as references for various `Number` constants. */
|
402 | var MAX_SAFE_INTEGER = 9007199254740991;
|
403 |
|
404 | /**
|
405 | * Checks if `value` is a valid array-like length.
|
406 | *
|
407 | * **Note:** This function is loosely based on
|
408 | * [`ToLength`](http://ecma-international.org/ecma-262/6.0/#sec-tolength).
|
409 | *
|
410 | * @static
|
411 | * @memberOf _
|
412 | * @since 4.0.0
|
413 | * @category Lang
|
414 | * @param {*} value The value to check.
|
415 | * @returns {boolean} Returns `true` if `value` is a valid length,
|
416 | * else `false`.
|
417 | * @example
|
418 | *
|
419 | * _.isLength(3);
|
420 | * // => true
|
421 | *
|
422 | * _.isLength(Number.MIN_VALUE);
|
423 | * // => false
|
424 | *
|
425 | * _.isLength(Infinity);
|
426 | * // => false
|
427 | *
|
428 | * _.isLength('3');
|
429 | * // => false
|
430 | */
|
431 | function isLength(value) {
|
432 | return typeof value == 'number' &&
|
433 | value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
|
434 | }
|
435 |
|
436 | /**
|
437 | * Checks if `value` is array-like. A value is considered array-like if it's
|
438 | * not a function and has a `value.length` that's an integer greater than or
|
439 | * equal to `0` and less than or equal to `Number.MAX_SAFE_INTEGER`.
|
440 | *
|
441 | * @static
|
442 | * @memberOf _
|
443 | * @since 4.0.0
|
444 | * @category Lang
|
445 | * @param {*} value The value to check.
|
446 | * @returns {boolean} Returns `true` if `value` is array-like, else `false`.
|
447 | * @example
|
448 | *
|
449 | * _.isArrayLike([1, 2, 3]);
|
450 | * // => true
|
451 | *
|
452 | * _.isArrayLike(document.body.children);
|
453 | * // => true
|
454 | *
|
455 | * _.isArrayLike('abc');
|
456 | * // => true
|
457 | *
|
458 | * _.isArrayLike(_.noop);
|
459 | * // => false
|
460 | */
|
461 | function isArrayLike(value) {
|
462 | return value != null && isLength(getLength(value)) && !isFunction(value);
|
463 | }
|
464 |
|
465 | /**
|
466 | * A method that returns `undefined`.
|
467 | *
|
468 | * @static
|
469 | * @memberOf _
|
470 | * @since 2.3.0
|
471 | * @category Util
|
472 | * @example
|
473 | *
|
474 | * _.times(2, _.noop);
|
475 | * // => [undefined, undefined]
|
476 | */
|
477 | function noop() {
|
478 | // No operation performed.
|
479 | }
|
480 |
|
481 | function once(fn) {
|
482 | return function () {
|
483 | if (fn === null) return;
|
484 | var callFn = fn;
|
485 | fn = null;
|
486 | callFn.apply(this, arguments);
|
487 | };
|
488 | }
|
489 |
|
490 | var iteratorSymbol = typeof Symbol === 'function' && Symbol.iterator;
|
491 |
|
492 | function getIterator (coll) {
|
493 | return iteratorSymbol && coll[iteratorSymbol] && coll[iteratorSymbol]();
|
494 | }
|
495 |
|
496 | /* Built-in method references for those with the same name as other `lodash` methods. */
|
497 | var nativeGetPrototype = Object.getPrototypeOf;
|
498 |
|
499 | /**
|
500 | * Gets the `[[Prototype]]` of `value`.
|
501 | *
|
502 | * @private
|
503 | * @param {*} value The value to query.
|
504 | * @returns {null|Object} Returns the `[[Prototype]]`.
|
505 | */
|
506 | function getPrototype(value) {
|
507 | return nativeGetPrototype(Object(value));
|
508 | }
|
509 |
|
510 | /** Used for built-in method references. */
|
511 | var objectProto$2 = Object.prototype;
|
512 |
|
513 | /** Used to check objects for own properties. */
|
514 | var hasOwnProperty = objectProto$2.hasOwnProperty;
|
515 |
|
516 | /**
|
517 | * The base implementation of `_.has` without support for deep paths.
|
518 | *
|
519 | * @private
|
520 | * @param {Object} [object] The object to query.
|
521 | * @param {Array|string} key The key to check.
|
522 | * @returns {boolean} Returns `true` if `key` exists, else `false`.
|
523 | */
|
524 | function baseHas(object, key) {
|
525 | // Avoid a bug in IE 10-11 where objects with a [[Prototype]] of `null`,
|
526 | // that are composed entirely of index properties, return `false` for
|
527 | // `hasOwnProperty` checks of them.
|
528 | return object != null &&
|
529 | (hasOwnProperty.call(object, key) ||
|
530 | (typeof object == 'object' && key in object && getPrototype(object) === null));
|
531 | }
|
532 |
|
533 | /* Built-in method references for those with the same name as other `lodash` methods. */
|
534 | var nativeKeys = Object.keys;
|
535 |
|
536 | /**
|
537 | * The base implementation of `_.keys` which doesn't skip the constructor
|
538 | * property of prototypes or treat sparse arrays as dense.
|
539 | *
|
540 | * @private
|
541 | * @param {Object} object The object to query.
|
542 | * @returns {Array} Returns the array of property names.
|
543 | */
|
544 | function baseKeys(object) {
|
545 | return nativeKeys(Object(object));
|
546 | }
|
547 |
|
548 | /**
|
549 | * The base implementation of `_.times` without support for iteratee shorthands
|
550 | * or max array length checks.
|
551 | *
|
552 | * @private
|
553 | * @param {number} n The number of times to invoke `iteratee`.
|
554 | * @param {Function} iteratee The function invoked per iteration.
|
555 | * @returns {Array} Returns the array of results.
|
556 | */
|
557 | function baseTimes(n, iteratee) {
|
558 | var index = -1,
|
559 | result = Array(n);
|
560 |
|
561 | while (++index < n) {
|
562 | result[index] = iteratee(index);
|
563 | }
|
564 | return result;
|
565 | }
|
566 |
|
567 | /**
|
568 | * This method is like `_.isArrayLike` except that it also checks if `value`
|
569 | * is an object.
|
570 | *
|
571 | * @static
|
572 | * @memberOf _
|
573 | * @since 4.0.0
|
574 | * @category Lang
|
575 | * @param {*} value The value to check.
|
576 | * @returns {boolean} Returns `true` if `value` is an array-like object,
|
577 | * else `false`.
|
578 | * @example
|
579 | *
|
580 | * _.isArrayLikeObject([1, 2, 3]);
|
581 | * // => true
|
582 | *
|
583 | * _.isArrayLikeObject(document.body.children);
|
584 | * // => true
|
585 | *
|
586 | * _.isArrayLikeObject('abc');
|
587 | * // => false
|
588 | *
|
589 | * _.isArrayLikeObject(_.noop);
|
590 | * // => false
|
591 | */
|
592 | function isArrayLikeObject(value) {
|
593 | return isObjectLike(value) && isArrayLike(value);
|
594 | }
|
595 |
|
596 | /** `Object#toString` result references. */
|
597 | var argsTag = '[object Arguments]';
|
598 |
|
599 | /** Used for built-in method references. */
|
600 | var objectProto$3 = Object.prototype;
|
601 |
|
602 | /** Used to check objects for own properties. */
|
603 | var hasOwnProperty$1 = objectProto$3.hasOwnProperty;
|
604 |
|
605 | /**
|
606 | * Used to resolve the
|
607 | * [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
|
608 | * of values.
|
609 | */
|
610 | var objectToString$2 = objectProto$3.toString;
|
611 |
|
612 | /** Built-in value references. */
|
613 | var propertyIsEnumerable = objectProto$3.propertyIsEnumerable;
|
614 |
|
615 | /**
|
616 | * Checks if `value` is likely an `arguments` object.
|
617 | *
|
618 | * @static
|
619 | * @memberOf _
|
620 | * @since 0.1.0
|
621 | * @category Lang
|
622 | * @param {*} value The value to check.
|
623 | * @returns {boolean} Returns `true` if `value` is correctly classified,
|
624 | * else `false`.
|
625 | * @example
|
626 | *
|
627 | * _.isArguments(function() { return arguments; }());
|
628 | * // => true
|
629 | *
|
630 | * _.isArguments([1, 2, 3]);
|
631 | * // => false
|
632 | */
|
633 | function isArguments(value) {
|
634 | // Safari 8.1 incorrectly makes `arguments.callee` enumerable in strict mode.
|
635 | return isArrayLikeObject(value) && hasOwnProperty$1.call(value, 'callee') &&
|
636 | (!propertyIsEnumerable.call(value, 'callee') || objectToString$2.call(value) == argsTag);
|
637 | }
|
638 |
|
639 | /**
|
640 | * Checks if `value` is classified as an `Array` object.
|
641 | *
|
642 | * @static
|
643 | * @memberOf _
|
644 | * @since 0.1.0
|
645 | * @type {Function}
|
646 | * @category Lang
|
647 | * @param {*} value The value to check.
|
648 | * @returns {boolean} Returns `true` if `value` is correctly classified,
|
649 | * else `false`.
|
650 | * @example
|
651 | *
|
652 | * _.isArray([1, 2, 3]);
|
653 | * // => true
|
654 | *
|
655 | * _.isArray(document.body.children);
|
656 | * // => false
|
657 | *
|
658 | * _.isArray('abc');
|
659 | * // => false
|
660 | *
|
661 | * _.isArray(_.noop);
|
662 | * // => false
|
663 | */
|
664 | var isArray = Array.isArray;
|
665 |
|
666 | /** `Object#toString` result references. */
|
667 | var stringTag = '[object String]';
|
668 |
|
669 | /** Used for built-in method references. */
|
670 | var objectProto$4 = Object.prototype;
|
671 |
|
672 | /**
|
673 | * Used to resolve the
|
674 | * [`toStringTag`](http://ecma-international.org/ecma-262/6.0/#sec-object.prototype.tostring)
|
675 | * of values.
|
676 | */
|
677 | var objectToString$3 = objectProto$4.toString;
|
678 |
|
679 | /**
|
680 | * Checks if `value` is classified as a `String` primitive or object.
|
681 | *
|
682 | * @static
|
683 | * @since 0.1.0
|
684 | * @memberOf _
|
685 | * @category Lang
|
686 | * @param {*} value The value to check.
|
687 | * @returns {boolean} Returns `true` if `value` is correctly classified,
|
688 | * else `false`.
|
689 | * @example
|
690 | *
|
691 | * _.isString('abc');
|
692 | * // => true
|
693 | *
|
694 | * _.isString(1);
|
695 | * // => false
|
696 | */
|
697 | function isString(value) {
|
698 | return typeof value == 'string' ||
|
699 | (!isArray(value) && isObjectLike(value) && objectToString$3.call(value) == stringTag);
|
700 | }
|
701 |
|
702 | /**
|
703 | * Creates an array of index keys for `object` values of arrays,
|
704 | * `arguments` objects, and strings, otherwise `null` is returned.
|
705 | *
|
706 | * @private
|
707 | * @param {Object} object The object to query.
|
708 | * @returns {Array|null} Returns index keys, else `null`.
|
709 | */
|
710 | function indexKeys(object) {
|
711 | var length = object ? object.length : undefined;
|
712 | if (isLength(length) &&
|
713 | (isArray(object) || isString(object) || isArguments(object))) {
|
714 | return baseTimes(length, String);
|
715 | }
|
716 | return null;
|
717 | }
|
718 |
|
719 | /** Used as references for various `Number` constants. */
|
720 | var MAX_SAFE_INTEGER$1 = 9007199254740991;
|
721 |
|
722 | /** Used to detect unsigned integer values. */
|
723 | var reIsUint = /^(?:0|[1-9]\d*)$/;
|
724 |
|
725 | /**
|
726 | * Checks if `value` is a valid array-like index.
|
727 | *
|
728 | * @private
|
729 | * @param {*} value The value to check.
|
730 | * @param {number} [length=MAX_SAFE_INTEGER] The upper bounds of a valid index.
|
731 | * @returns {boolean} Returns `true` if `value` is a valid index, else `false`.
|
732 | */
|
733 | function isIndex(value, length) {
|
734 | length = length == null ? MAX_SAFE_INTEGER$1 : length;
|
735 | return !!length &&
|
736 | (typeof value == 'number' || reIsUint.test(value)) &&
|
737 | (value > -1 && value % 1 == 0 && value < length);
|
738 | }
|
739 |
|
740 | /** Used for built-in method references. */
|
741 | var objectProto$5 = Object.prototype;
|
742 |
|
743 | /**
|
744 | * Checks if `value` is likely a prototype object.
|
745 | *
|
746 | * @private
|
747 | * @param {*} value The value to check.
|
748 | * @returns {boolean} Returns `true` if `value` is a prototype, else `false`.
|
749 | */
|
750 | function isPrototype(value) {
|
751 | var Ctor = value && value.constructor,
|
752 | proto = (typeof Ctor == 'function' && Ctor.prototype) || objectProto$5;
|
753 |
|
754 | return value === proto;
|
755 | }
|
756 |
|
757 | /**
|
758 | * Creates an array of the own enumerable property names of `object`.
|
759 | *
|
760 | * **Note:** Non-object values are coerced to objects. See the
|
761 | * [ES spec](http://ecma-international.org/ecma-262/6.0/#sec-object.keys)
|
762 | * for more details.
|
763 | *
|
764 | * @static
|
765 | * @since 0.1.0
|
766 | * @memberOf _
|
767 | * @category Object
|
768 | * @param {Object} object The object to query.
|
769 | * @returns {Array} Returns the array of property names.
|
770 | * @example
|
771 | *
|
772 | * function Foo() {
|
773 | * this.a = 1;
|
774 | * this.b = 2;
|
775 | * }
|
776 | *
|
777 | * Foo.prototype.c = 3;
|
778 | *
|
779 | * _.keys(new Foo);
|
780 | * // => ['a', 'b'] (iteration order is not guaranteed)
|
781 | *
|
782 | * _.keys('hi');
|
783 | * // => ['0', '1']
|
784 | */
|
785 | function keys(object) {
|
786 | var isProto = isPrototype(object);
|
787 | if (!(isProto || isArrayLike(object))) {
|
788 | return baseKeys(object);
|
789 | }
|
790 | var indexes = indexKeys(object),
|
791 | skipIndexes = !!indexes,
|
792 | result = indexes || [],
|
793 | length = result.length;
|
794 |
|
795 | for (var key in object) {
|
796 | if (baseHas(object, key) &&
|
797 | !(skipIndexes && (key == 'length' || isIndex(key, length))) &&
|
798 | !(isProto && key == 'constructor')) {
|
799 | result.push(key);
|
800 | }
|
801 | }
|
802 | return result;
|
803 | }
|
804 |
|
805 | function createArrayIterator(coll) {
|
806 | var i = -1;
|
807 | var len = coll.length;
|
808 | return function next() {
|
809 | return ++i < len ? { value: coll[i], key: i } : null;
|
810 | };
|
811 | }
|
812 |
|
813 | function createES2015Iterator(iterator) {
|
814 | var i = -1;
|
815 | return function next() {
|
816 | var item = iterator.next();
|
817 | if (item.done) return null;
|
818 | i++;
|
819 | return { value: item.value, key: i };
|
820 | };
|
821 | }
|
822 |
|
823 | function createObjectIterator(obj) {
|
824 | var okeys = keys(obj);
|
825 | var i = -1;
|
826 | var len = okeys.length;
|
827 | return function next() {
|
828 | var key = okeys[++i];
|
829 | return i < len ? { value: obj[key], key: key } : null;
|
830 | };
|
831 | }
|
832 |
|
833 | function iterator(coll) {
|
834 | if (isArrayLike(coll)) {
|
835 | return createArrayIterator(coll);
|
836 | }
|
837 |
|
838 | var iterator = getIterator(coll);
|
839 | return iterator ? createES2015Iterator(iterator) : createObjectIterator(coll);
|
840 | }
|
841 |
|
842 | function onlyOnce(fn) {
|
843 | return function () {
|
844 | if (fn === null) throw new Error("Callback was already called.");
|
845 | var callFn = fn;
|
846 | fn = null;
|
847 | callFn.apply(this, arguments);
|
848 | };
|
849 | }
|
850 |
|
851 | function _eachOfLimit(limit) {
|
852 | return function (obj, iteratee, callback) {
|
853 | callback = once(callback || noop);
|
854 | if (limit <= 0 || !obj) {
|
855 | return callback(null);
|
856 | }
|
857 | var nextElem = iterator(obj);
|
858 | var done = false;
|
859 | var running = 0;
|
860 |
|
861 | function iterateeCallback(err) {
|
862 | running -= 1;
|
863 | if (err) {
|
864 | done = true;
|
865 | callback(err);
|
866 | } else if (done && running <= 0) {
|
867 | return callback(null);
|
868 | } else {
|
869 | replenish();
|
870 | }
|
871 | }
|
872 |
|
873 | function replenish() {
|
874 | while (running < limit && !done) {
|
875 | var elem = nextElem();
|
876 | if (elem === null) {
|
877 | done = true;
|
878 | if (running <= 0) {
|
879 | callback(null);
|
880 | }
|
881 | return;
|
882 | }
|
883 | running += 1;
|
884 | iteratee(elem.value, elem.key, onlyOnce(iterateeCallback));
|
885 | }
|
886 | }
|
887 |
|
888 | replenish();
|
889 | };
|
890 | }
|
891 |
|
892 | /**
|
893 | * The same as [`eachOf`]{@link module:Collections.eachOf} but runs a maximum of `limit` async operations at a
|
894 | * time.
|
895 | *
|
896 | * @name eachOfLimit
|
897 | * @static
|
898 | * @memberOf module:Collections
|
899 | * @method
|
900 | * @see [async.eachOf]{@link module:Collections.eachOf}
|
901 | * @alias forEachOfLimit
|
902 | * @category Collection
|
903 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
904 | * @param {number} limit - The maximum number of async operations at a time.
|
905 | * @param {Function} iteratee - A function to apply to each
|
906 | * item in `coll`. The `key` is the item's key, or index in the case of an
|
907 | * array. The iteratee is passed a `callback(err)` which must be called once it
|
908 | * has completed. If no error has occurred, the callback should be run without
|
909 | * arguments or with an explicit `null` argument. Invoked with
|
910 | * (item, key, callback).
|
911 | * @param {Function} [callback] - A callback which is called when all
|
912 | * `iteratee` functions have finished, or an error occurs. Invoked with (err).
|
913 | */
|
914 | function eachOfLimit(coll, limit, iteratee, callback) {
|
915 | _eachOfLimit(limit)(coll, iteratee, callback);
|
916 | }
|
917 |
|
918 | function doLimit(fn, limit) {
|
919 | return function (iterable, iteratee, callback) {
|
920 | return fn(iterable, limit, iteratee, callback);
|
921 | };
|
922 | }
|
923 |
|
924 | /** Used as the `TypeError` message for "Functions" methods. */
|
925 | var FUNC_ERROR_TEXT$1 = 'Expected a function';
|
926 |
|
927 | /**
|
928 | * Creates a function that invokes `func`, with the `this` binding and arguments
|
929 | * of the created function, while it's called less than `n` times. Subsequent
|
930 | * calls to the created function return the result of the last `func` invocation.
|
931 | *
|
932 | * @static
|
933 | * @memberOf _
|
934 | * @since 3.0.0
|
935 | * @category Function
|
936 | * @param {number} n The number of calls at which `func` is no longer invoked.
|
937 | * @param {Function} func The function to restrict.
|
938 | * @returns {Function} Returns the new restricted function.
|
939 | * @example
|
940 | *
|
941 | * jQuery(element).on('click', _.before(5, addContactToList));
|
942 | * // => allows adding up to 4 contacts to the list
|
943 | */
|
944 | function before(n, func) {
|
945 | var result;
|
946 | if (typeof func != 'function') {
|
947 | throw new TypeError(FUNC_ERROR_TEXT$1);
|
948 | }
|
949 | n = toInteger(n);
|
950 | return function() {
|
951 | if (--n > 0) {
|
952 | result = func.apply(this, arguments);
|
953 | }
|
954 | if (n <= 1) {
|
955 | func = undefined;
|
956 | }
|
957 | return result;
|
958 | };
|
959 | }
|
960 |
|
961 | /**
|
962 | * Creates a function that is restricted to invoking `func` once. Repeat calls
|
963 | * to the function return the value of the first invocation. The `func` is
|
964 | * invoked with the `this` binding and arguments of the created function.
|
965 | *
|
966 | * @static
|
967 | * @memberOf _
|
968 | * @since 0.1.0
|
969 | * @category Function
|
970 | * @param {Function} func The function to restrict.
|
971 | * @returns {Function} Returns the new restricted function.
|
972 | * @example
|
973 | *
|
974 | * var initialize = _.once(createApplication);
|
975 | * initialize();
|
976 | * initialize();
|
977 | * // `initialize` invokes `createApplication` once
|
978 | */
|
979 | function once$1(func) {
|
980 | return before(2, func);
|
981 | }
|
982 |
|
983 | // eachOf implementation optimized for array-likes
|
984 | function eachOfArrayLike(coll, iteratee, callback) {
|
985 | callback = once$1(callback || noop);
|
986 | var index = 0,
|
987 | completed = 0,
|
988 | length = coll.length;
|
989 | if (length === 0) {
|
990 | callback(null);
|
991 | }
|
992 |
|
993 | function iteratorCallback(err) {
|
994 | if (err) {
|
995 | callback(err);
|
996 | } else if (++completed === length) {
|
997 | callback(null);
|
998 | }
|
999 | }
|
1000 |
|
1001 | for (; index < length; index++) {
|
1002 | iteratee(coll[index], index, onlyOnce(iteratorCallback));
|
1003 | }
|
1004 | }
|
1005 |
|
1006 | // a generic version of eachOf which can handle array, object, and iterator cases.
|
1007 | var eachOfGeneric = doLimit(eachOfLimit, Infinity);
|
1008 |
|
1009 | /**
|
1010 | * Like [`each`]{@link module:Collections.each}, except that it passes the key (or index) as the second argument
|
1011 | * to the iteratee.
|
1012 | *
|
1013 | * @name eachOf
|
1014 | * @static
|
1015 | * @memberOf module:Collections
|
1016 | * @method
|
1017 | * @alias forEachOf
|
1018 | * @category Collection
|
1019 | * @see [async.each]{@link module:Collections.each}
|
1020 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
1021 | * @param {Function} iteratee - A function to apply to each
|
1022 | * item in `coll`. The `key` is the item's key, or index in the case of an
|
1023 | * array. The iteratee is passed a `callback(err)` which must be called once it
|
1024 | * has completed. If no error has occurred, the callback should be run without
|
1025 | * arguments or with an explicit `null` argument. Invoked with
|
1026 | * (item, key, callback).
|
1027 | * @param {Function} [callback] - A callback which is called when all
|
1028 | * `iteratee` functions have finished, or an error occurs. Invoked with (err).
|
1029 | * @example
|
1030 | *
|
1031 | * var obj = {dev: "/dev.json", test: "/test.json", prod: "/prod.json"};
|
1032 | * var configs = {};
|
1033 | *
|
1034 | * async.forEachOf(obj, function (value, key, callback) {
|
1035 | * fs.readFile(__dirname + value, "utf8", function (err, data) {
|
1036 | * if (err) return callback(err);
|
1037 | * try {
|
1038 | * configs[key] = JSON.parse(data);
|
1039 | * } catch (e) {
|
1040 | * return callback(e);
|
1041 | * }
|
1042 | * callback();
|
1043 | * });
|
1044 | * }, function (err) {
|
1045 | * if (err) console.error(err.message);
|
1046 | * // configs is now a map of JSON data
|
1047 | * doSomethingWith(configs);
|
1048 | * });
|
1049 | */
|
1050 | function eachOf (coll, iteratee, callback) {
|
1051 | var eachOfImplementation = isArrayLike(coll) ? eachOfArrayLike : eachOfGeneric;
|
1052 | eachOfImplementation(coll, iteratee, callback);
|
1053 | }
|
1054 |
|
1055 | function doParallel(fn) {
|
1056 | return function (obj, iteratee, callback) {
|
1057 | return fn(eachOf, obj, iteratee, callback);
|
1058 | };
|
1059 | }
|
1060 |
|
1061 | function _asyncMap(eachfn, arr, iteratee, callback) {
|
1062 | callback = once(callback || noop);
|
1063 | arr = arr || [];
|
1064 | var results = [];
|
1065 | var counter = 0;
|
1066 |
|
1067 | eachfn(arr, function (value, _, callback) {
|
1068 | var index = counter++;
|
1069 | iteratee(value, function (err, v) {
|
1070 | results[index] = v;
|
1071 | callback(err);
|
1072 | });
|
1073 | }, function (err) {
|
1074 | callback(err, results);
|
1075 | });
|
1076 | }
|
1077 |
|
1078 | /**
|
1079 | * Produces a new collection of values by mapping each value in `coll` through
|
1080 | * the `iteratee` function. The `iteratee` is called with an item from `coll`
|
1081 | * and a callback for when it has finished processing. Each of these callback
|
1082 | * takes 2 arguments: an `error`, and the transformed item from `coll`. If
|
1083 | * `iteratee` passes an error to its callback, the main `callback` (for the
|
1084 | * `map` function) is immediately called with the error.
|
1085 | *
|
1086 | * Note, that since this function applies the `iteratee` to each item in
|
1087 | * parallel, there is no guarantee that the `iteratee` functions will complete
|
1088 | * in order. However, the results array will be in the same order as the
|
1089 | * original `coll`.
|
1090 | *
|
1091 | * If `map` is passed an Object, the results will be an Array. The results
|
1092 | * will roughly be in the order of the original Objects' keys (but this can
|
1093 | * vary across JavaScript engines)
|
1094 | *
|
1095 | * @name map
|
1096 | * @static
|
1097 | * @memberOf module:Collections
|
1098 | * @method
|
1099 | * @category Collection
|
1100 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
1101 | * @param {Function} iteratee - A function to apply to each item in `coll`.
|
1102 | * The iteratee is passed a `callback(err, transformed)` which must be called
|
1103 | * once it has completed with an error (which can be `null`) and a
|
1104 | * transformed item. Invoked with (item, callback).
|
1105 | * @param {Function} [callback] - A callback which is called when all `iteratee`
|
1106 | * functions have finished, or an error occurs. Results is an Array of the
|
1107 | * transformed items from the `coll`. Invoked with (err, results).
|
1108 | * @example
|
1109 | *
|
1110 | * async.map(['file1','file2','file3'], fs.stat, function(err, results) {
|
1111 | * // results is now an array of stats for each file
|
1112 | * });
|
1113 | */
|
1114 | var map = doParallel(_asyncMap);
|
1115 |
|
1116 | /**
|
1117 | * Applies the provided arguments to each function in the array, calling
|
1118 | * `callback` after all functions have completed. If you only provide the first
|
1119 | * argument, then it will return a function which lets you pass in the
|
1120 | * arguments as if it were a single function call.
|
1121 | *
|
1122 | * @name applyEach
|
1123 | * @static
|
1124 | * @memberOf module:ControlFlow
|
1125 | * @method
|
1126 | * @category Control Flow
|
1127 | * @param {Array|Iterable|Object} fns - A collection of asynchronous functions to all
|
1128 | * call with the same arguments
|
1129 | * @param {...*} [args] - any number of separate arguments to pass to the
|
1130 | * function.
|
1131 | * @param {Function} [callback] - the final argument should be the callback,
|
1132 | * called when all functions have completed processing.
|
1133 | * @returns {Function} - If only the first argument is provided, it will return
|
1134 | * a function which lets you pass in the arguments as if it were a single
|
1135 | * function call.
|
1136 | * @example
|
1137 | *
|
1138 | * async.applyEach([enableSearch, updateSchema], 'bucket', callback);
|
1139 | *
|
1140 | * // partial application example:
|
1141 | * async.each(
|
1142 | * buckets,
|
1143 | * async.applyEach([enableSearch, updateSchema]),
|
1144 | * callback
|
1145 | * );
|
1146 | */
|
1147 | var applyEach = applyEach$1(map);
|
1148 |
|
1149 | function doParallelLimit(fn) {
|
1150 | return function (obj, limit, iteratee, callback) {
|
1151 | return fn(_eachOfLimit(limit), obj, iteratee, callback);
|
1152 | };
|
1153 | }
|
1154 |
|
1155 | /**
|
1156 | * The same as [`map`]{@link module:Collections.map} but runs a maximum of `limit` async operations at a time.
|
1157 | *
|
1158 | * @name mapLimit
|
1159 | * @static
|
1160 | * @memberOf module:Collections
|
1161 | * @method
|
1162 | * @see [async.map]{@link module:Collections.map}
|
1163 | * @category Collection
|
1164 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
1165 | * @param {number} limit - The maximum number of async operations at a time.
|
1166 | * @param {Function} iteratee - A function to apply to each item in `coll`.
|
1167 | * The iteratee is passed a `callback(err, transformed)` which must be called
|
1168 | * once it has completed with an error (which can be `null`) and a transformed
|
1169 | * item. Invoked with (item, callback).
|
1170 | * @param {Function} [callback] - A callback which is called when all `iteratee`
|
1171 | * functions have finished, or an error occurs. Results is an array of the
|
1172 | * transformed items from the `coll`. Invoked with (err, results).
|
1173 | */
|
1174 | var mapLimit = doParallelLimit(_asyncMap);
|
1175 |
|
1176 | /**
|
1177 | * The same as [`map`]{@link module:Collections.map} but runs only a single async operation at a time.
|
1178 | *
|
1179 | * @name mapSeries
|
1180 | * @static
|
1181 | * @memberOf module:Collections
|
1182 | * @method
|
1183 | * @see [async.map]{@link module:Collections.map}
|
1184 | * @category Collection
|
1185 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
1186 | * @param {Function} iteratee - A function to apply to each item in `coll`.
|
1187 | * The iteratee is passed a `callback(err, transformed)` which must be called
|
1188 | * once it has completed with an error (which can be `null`) and a
|
1189 | * transformed item. Invoked with (item, callback).
|
1190 | * @param {Function} [callback] - A callback which is called when all `iteratee`
|
1191 | * functions have finished, or an error occurs. Results is an array of the
|
1192 | * transformed items from the `coll`. Invoked with (err, results).
|
1193 | */
|
1194 | var mapSeries = doLimit(mapLimit, 1);
|
1195 |
|
1196 | /**
|
1197 | * The same as [`applyEach`]{@link module:ControlFlow.applyEach} but runs only a single async operation at a time.
|
1198 | *
|
1199 | * @name applyEachSeries
|
1200 | * @static
|
1201 | * @memberOf module:ControlFlow
|
1202 | * @method
|
1203 | * @see [async.applyEach]{@link module:ControlFlow.applyEach}
|
1204 | * @category Control Flow
|
1205 | * @param {Array|Iterable|Object} fns - A collection of asynchronous functions to all
|
1206 | * call with the same arguments
|
1207 | * @param {...*} [args] - any number of separate arguments to pass to the
|
1208 | * function.
|
1209 | * @param {Function} [callback] - the final argument should be the callback,
|
1210 | * called when all functions have completed processing.
|
1211 | * @returns {Function} - If only the first argument is provided, it will return
|
1212 | * a function which lets you pass in the arguments as if it were a single
|
1213 | * function call.
|
1214 | */
|
1215 | var applyEachSeries = applyEach$1(mapSeries);
|
1216 |
|
1217 | /**
|
1218 | * Creates a continuation function with some arguments already applied.
|
1219 | *
|
1220 | * Useful as a shorthand when combined with other control flow functions. Any
|
1221 | * arguments passed to the returned function are added to the arguments
|
1222 | * originally passed to apply.
|
1223 | *
|
1224 | * @name apply
|
1225 | * @static
|
1226 | * @memberOf module:Utils
|
1227 | * @method
|
1228 | * @category Util
|
1229 | * @param {Function} function - The function you want to eventually apply all
|
1230 | * arguments to. Invokes with (arguments...).
|
1231 | * @param {...*} arguments... - Any number of arguments to automatically apply
|
1232 | * when the continuation is called.
|
1233 | * @example
|
1234 | *
|
1235 | * // using apply
|
1236 | * async.parallel([
|
1237 | * async.apply(fs.writeFile, 'testfile1', 'test1'),
|
1238 | * async.apply(fs.writeFile, 'testfile2', 'test2')
|
1239 | * ]);
|
1240 | *
|
1241 | *
|
1242 | * // the same process without using apply
|
1243 | * async.parallel([
|
1244 | * function(callback) {
|
1245 | * fs.writeFile('testfile1', 'test1', callback);
|
1246 | * },
|
1247 | * function(callback) {
|
1248 | * fs.writeFile('testfile2', 'test2', callback);
|
1249 | * }
|
1250 | * ]);
|
1251 | *
|
1252 | * // It's possible to pass any number of additional arguments when calling the
|
1253 | * // continuation:
|
1254 | *
|
1255 | * node> var fn = async.apply(sys.puts, 'one');
|
1256 | * node> fn('two', 'three');
|
1257 | * one
|
1258 | * two
|
1259 | * three
|
1260 | */
|
1261 | var apply$1 = rest(function (fn, args) {
|
1262 | return rest(function (callArgs) {
|
1263 | return fn.apply(null, args.concat(callArgs));
|
1264 | });
|
1265 | });
|
1266 |
|
1267 | /**
|
1268 | * Take a sync function and make it async, passing its return value to a
|
1269 | * callback. This is useful for plugging sync functions into a waterfall,
|
1270 | * series, or other async functions. Any arguments passed to the generated
|
1271 | * function will be passed to the wrapped function (except for the final
|
1272 | * callback argument). Errors thrown will be passed to the callback.
|
1273 | *
|
1274 | * If the function passed to `asyncify` returns a Promise, that promises's
|
1275 | * resolved/rejected state will be used to call the callback, rather than simply
|
1276 | * the synchronous return value.
|
1277 | *
|
1278 | * This also means you can asyncify ES2016 `async` functions.
|
1279 | *
|
1280 | * @name asyncify
|
1281 | * @static
|
1282 | * @memberOf module:Utils
|
1283 | * @method
|
1284 | * @alias wrapSync
|
1285 | * @category Util
|
1286 | * @param {Function} func - The synchronous function to convert to an
|
1287 | * asynchronous function.
|
1288 | * @returns {Function} An asynchronous wrapper of the `func`. To be invoked with
|
1289 | * (callback).
|
1290 | * @example
|
1291 | *
|
1292 | * // passing a regular synchronous function
|
1293 | * async.waterfall([
|
1294 | * async.apply(fs.readFile, filename, "utf8"),
|
1295 | * async.asyncify(JSON.parse),
|
1296 | * function (data, next) {
|
1297 | * // data is the result of parsing the text.
|
1298 | * // If there was a parsing error, it would have been caught.
|
1299 | * }
|
1300 | * ], callback);
|
1301 | *
|
1302 | * // passing a function returning a promise
|
1303 | * async.waterfall([
|
1304 | * async.apply(fs.readFile, filename, "utf8"),
|
1305 | * async.asyncify(function (contents) {
|
1306 | * return db.model.create(contents);
|
1307 | * }),
|
1308 | * function (model, next) {
|
1309 | * // `model` is the instantiated model object.
|
1310 | * // If there was an error, this function would be skipped.
|
1311 | * }
|
1312 | * ], callback);
|
1313 | *
|
1314 | * // es6 example
|
1315 | * var q = async.queue(async.asyncify(async function(file) {
|
1316 | * var intermediateStep = await processFile(file);
|
1317 | * return await somePromise(intermediateStep)
|
1318 | * }));
|
1319 | *
|
1320 | * q.push(files);
|
1321 | */
|
1322 | function asyncify(func) {
|
1323 | return initialParams(function (args, callback) {
|
1324 | var result;
|
1325 | try {
|
1326 | result = func.apply(this, args);
|
1327 | } catch (e) {
|
1328 | return callback(e);
|
1329 | }
|
1330 | // if result is Promise object
|
1331 | if (isObject(result) && typeof result.then === 'function') {
|
1332 | result.then(function (value) {
|
1333 | callback(null, value);
|
1334 | }, function (err) {
|
1335 | callback(err.message ? err : new Error(err));
|
1336 | });
|
1337 | } else {
|
1338 | callback(null, result);
|
1339 | }
|
1340 | });
|
1341 | }
|
1342 |
|
1343 | /**
|
1344 | * A specialized version of `_.forEach` for arrays without support for
|
1345 | * iteratee shorthands.
|
1346 | *
|
1347 | * @private
|
1348 | * @param {Array} [array] The array to iterate over.
|
1349 | * @param {Function} iteratee The function invoked per iteration.
|
1350 | * @returns {Array} Returns `array`.
|
1351 | */
|
1352 | function arrayEach(array, iteratee) {
|
1353 | var index = -1,
|
1354 | length = array ? array.length : 0;
|
1355 |
|
1356 | while (++index < length) {
|
1357 | if (iteratee(array[index], index, array) === false) {
|
1358 | break;
|
1359 | }
|
1360 | }
|
1361 | return array;
|
1362 | }
|
1363 |
|
1364 | /**
|
1365 | * Creates a base function for methods like `_.forIn` and `_.forOwn`.
|
1366 | *
|
1367 | * @private
|
1368 | * @param {boolean} [fromRight] Specify iterating from right to left.
|
1369 | * @returns {Function} Returns the new base function.
|
1370 | */
|
1371 | function createBaseFor(fromRight) {
|
1372 | return function(object, iteratee, keysFunc) {
|
1373 | var index = -1,
|
1374 | iterable = Object(object),
|
1375 | props = keysFunc(object),
|
1376 | length = props.length;
|
1377 |
|
1378 | while (length--) {
|
1379 | var key = props[fromRight ? length : ++index];
|
1380 | if (iteratee(iterable[key], key, iterable) === false) {
|
1381 | break;
|
1382 | }
|
1383 | }
|
1384 | return object;
|
1385 | };
|
1386 | }
|
1387 |
|
1388 | /**
|
1389 | * The base implementation of `baseForOwn` which iterates over `object`
|
1390 | * properties returned by `keysFunc` and invokes `iteratee` for each property.
|
1391 | * Iteratee functions may exit iteration early by explicitly returning `false`.
|
1392 | *
|
1393 | * @private
|
1394 | * @param {Object} object The object to iterate over.
|
1395 | * @param {Function} iteratee The function invoked per iteration.
|
1396 | * @param {Function} keysFunc The function to get the keys of `object`.
|
1397 | * @returns {Object} Returns `object`.
|
1398 | */
|
1399 | var baseFor = createBaseFor();
|
1400 |
|
1401 | /**
|
1402 | * The base implementation of `_.forOwn` without support for iteratee shorthands.
|
1403 | *
|
1404 | * @private
|
1405 | * @param {Object} object The object to iterate over.
|
1406 | * @param {Function} iteratee The function invoked per iteration.
|
1407 | * @returns {Object} Returns `object`.
|
1408 | */
|
1409 | function baseForOwn(object, iteratee) {
|
1410 | return object && baseFor(object, iteratee, keys);
|
1411 | }
|
1412 |
|
1413 | /**
|
1414 | * Gets the index at which the first occurrence of `NaN` is found in `array`.
|
1415 | *
|
1416 | * @private
|
1417 | * @param {Array} array The array to search.
|
1418 | * @param {number} fromIndex The index to search from.
|
1419 | * @param {boolean} [fromRight] Specify iterating from right to left.
|
1420 | * @returns {number} Returns the index of the matched `NaN`, else `-1`.
|
1421 | */
|
1422 | function indexOfNaN(array, fromIndex, fromRight) {
|
1423 | var length = array.length,
|
1424 | index = fromIndex + (fromRight ? 1 : -1);
|
1425 |
|
1426 | while ((fromRight ? index-- : ++index < length)) {
|
1427 | var other = array[index];
|
1428 | if (other !== other) {
|
1429 | return index;
|
1430 | }
|
1431 | }
|
1432 | return -1;
|
1433 | }
|
1434 |
|
1435 | /**
|
1436 | * The base implementation of `_.indexOf` without `fromIndex` bounds checks.
|
1437 | *
|
1438 | * @private
|
1439 | * @param {Array} array The array to search.
|
1440 | * @param {*} value The value to search for.
|
1441 | * @param {number} fromIndex The index to search from.
|
1442 | * @returns {number} Returns the index of the matched value, else `-1`.
|
1443 | */
|
1444 | function baseIndexOf(array, value, fromIndex) {
|
1445 | if (value !== value) {
|
1446 | return indexOfNaN(array, fromIndex);
|
1447 | }
|
1448 | var index = fromIndex - 1,
|
1449 | length = array.length;
|
1450 |
|
1451 | while (++index < length) {
|
1452 | if (array[index] === value) {
|
1453 | return index;
|
1454 | }
|
1455 | }
|
1456 | return -1;
|
1457 | }
|
1458 |
|
1459 | /**
|
1460 | * Determines the best order for running the functions in `tasks`, based on
|
1461 | * their requirements. Each function can optionally depend on other functions
|
1462 | * being completed first, and each function is run as soon as its requirements
|
1463 | * are satisfied.
|
1464 | *
|
1465 | * If any of the functions pass an error to their callback, the `auto` sequence
|
1466 | * will stop. Further tasks will not execute (so any other functions depending
|
1467 | * on it will not run), and the main `callback` is immediately called with the
|
1468 | * error.
|
1469 | *
|
1470 | * Functions also receive an object containing the results of functions which
|
1471 | * have completed so far as the first argument, if they have dependencies. If a
|
1472 | * task function has no dependencies, it will only be passed a callback.
|
1473 | *
|
1474 | * @name auto
|
1475 | * @static
|
1476 | * @memberOf module:ControlFlow
|
1477 | * @method
|
1478 | * @category Control Flow
|
1479 | * @param {Object} tasks - An object. Each of its properties is either a
|
1480 | * function or an array of requirements, with the function itself the last item
|
1481 | * in the array. The object's key of a property serves as the name of the task
|
1482 | * defined by that property, i.e. can be used when specifying requirements for
|
1483 | * other tasks. The function receives one or two arguments:
|
1484 | * * a `results` object, containing the results of the previously executed
|
1485 | * functions, only passed if the task has any dependencies,
|
1486 | * * a `callback(err, result)` function, which must be called when finished,
|
1487 | * passing an `error` (which can be `null`) and the result of the function's
|
1488 | * execution.
|
1489 | * @param {number} [concurrency=Infinity] - An optional `integer` for
|
1490 | * determining the maximum number of tasks that can be run in parallel. By
|
1491 | * default, as many as possible.
|
1492 | * @param {Function} [callback] - An optional callback which is called when all
|
1493 | * the tasks have been completed. It receives the `err` argument if any `tasks`
|
1494 | * pass an error to their callback. Results are always returned; however, if an
|
1495 | * error occurs, no further `tasks` will be performed, and the results object
|
1496 | * will only contain partial results. Invoked with (err, results).
|
1497 | * @returns undefined
|
1498 | * @example
|
1499 | *
|
1500 | * async.auto({
|
1501 | * // this function will just be passed a callback
|
1502 | * readData: async.apply(fs.readFile, 'data.txt', 'utf-8'),
|
1503 | * showData: ['readData', function(results, cb) {
|
1504 | * // results.readData is the file's contents
|
1505 | * // ...
|
1506 | * }]
|
1507 | * }, callback);
|
1508 | *
|
1509 | * async.auto({
|
1510 | * get_data: function(callback) {
|
1511 | * console.log('in get_data');
|
1512 | * // async code to get some data
|
1513 | * callback(null, 'data', 'converted to array');
|
1514 | * },
|
1515 | * make_folder: function(callback) {
|
1516 | * console.log('in make_folder');
|
1517 | * // async code to create a directory to store a file in
|
1518 | * // this is run at the same time as getting the data
|
1519 | * callback(null, 'folder');
|
1520 | * },
|
1521 | * write_file: ['get_data', 'make_folder', function(results, callback) {
|
1522 | * console.log('in write_file', JSON.stringify(results));
|
1523 | * // once there is some data and the directory exists,
|
1524 | * // write the data to a file in the directory
|
1525 | * callback(null, 'filename');
|
1526 | * }],
|
1527 | * email_link: ['write_file', function(results, callback) {
|
1528 | * console.log('in email_link', JSON.stringify(results));
|
1529 | * // once the file is written let's email a link to it...
|
1530 | * // results.write_file contains the filename returned by write_file.
|
1531 | * callback(null, {'file':results.write_file, 'email':'user@example.com'});
|
1532 | * }]
|
1533 | * }, function(err, results) {
|
1534 | * console.log('err = ', err);
|
1535 | * console.log('results = ', results);
|
1536 | * });
|
1537 | */
|
1538 | function auto (tasks, concurrency, callback) {
|
1539 | if (typeof concurrency === 'function') {
|
1540 | // concurrency is optional, shift the args.
|
1541 | callback = concurrency;
|
1542 | concurrency = null;
|
1543 | }
|
1544 | callback = once(callback || noop);
|
1545 | var keys$$ = keys(tasks);
|
1546 | var numTasks = keys$$.length;
|
1547 | if (!numTasks) {
|
1548 | return callback(null);
|
1549 | }
|
1550 | if (!concurrency) {
|
1551 | concurrency = numTasks;
|
1552 | }
|
1553 |
|
1554 | var results = {};
|
1555 | var runningTasks = 0;
|
1556 | var hasError = false;
|
1557 |
|
1558 | var listeners = {};
|
1559 |
|
1560 | var readyTasks = [];
|
1561 |
|
1562 | // for cycle detection:
|
1563 | var readyToCheck = []; // tasks that have been identified as reachable
|
1564 | // without the possibility of returning to an ancestor task
|
1565 | var uncheckedDependencies = {};
|
1566 |
|
1567 | baseForOwn(tasks, function (task, key) {
|
1568 | if (!isArray(task)) {
|
1569 | // no dependencies
|
1570 | enqueueTask(key, [task]);
|
1571 | readyToCheck.push(key);
|
1572 | return;
|
1573 | }
|
1574 |
|
1575 | var dependencies = task.slice(0, task.length - 1);
|
1576 | var remainingDependencies = dependencies.length;
|
1577 | if (remainingDependencies === 0) {
|
1578 | enqueueTask(key, task);
|
1579 | readyToCheck.push(key);
|
1580 | return;
|
1581 | }
|
1582 | uncheckedDependencies[key] = remainingDependencies;
|
1583 |
|
1584 | arrayEach(dependencies, function (dependencyName) {
|
1585 | if (!tasks[dependencyName]) {
|
1586 | throw new Error('async.auto task `' + key + '` has a non-existent dependency in ' + dependencies.join(', '));
|
1587 | }
|
1588 | addListener(dependencyName, function () {
|
1589 | remainingDependencies--;
|
1590 | if (remainingDependencies === 0) {
|
1591 | enqueueTask(key, task);
|
1592 | }
|
1593 | });
|
1594 | });
|
1595 | });
|
1596 |
|
1597 | checkForDeadlocks();
|
1598 | processQueue();
|
1599 |
|
1600 | function enqueueTask(key, task) {
|
1601 | readyTasks.push(function () {
|
1602 | runTask(key, task);
|
1603 | });
|
1604 | }
|
1605 |
|
1606 | function processQueue() {
|
1607 | if (readyTasks.length === 0 && runningTasks === 0) {
|
1608 | return callback(null, results);
|
1609 | }
|
1610 | while (readyTasks.length && runningTasks < concurrency) {
|
1611 | var run = readyTasks.shift();
|
1612 | run();
|
1613 | }
|
1614 | }
|
1615 |
|
1616 | function addListener(taskName, fn) {
|
1617 | var taskListeners = listeners[taskName];
|
1618 | if (!taskListeners) {
|
1619 | taskListeners = listeners[taskName] = [];
|
1620 | }
|
1621 |
|
1622 | taskListeners.push(fn);
|
1623 | }
|
1624 |
|
1625 | function taskComplete(taskName) {
|
1626 | var taskListeners = listeners[taskName] || [];
|
1627 | arrayEach(taskListeners, function (fn) {
|
1628 | fn();
|
1629 | });
|
1630 | processQueue();
|
1631 | }
|
1632 |
|
1633 | function runTask(key, task) {
|
1634 | if (hasError) return;
|
1635 |
|
1636 | var taskCallback = onlyOnce(rest(function (err, args) {
|
1637 | runningTasks--;
|
1638 | if (args.length <= 1) {
|
1639 | args = args[0];
|
1640 | }
|
1641 | if (err) {
|
1642 | var safeResults = {};
|
1643 | baseForOwn(results, function (val, rkey) {
|
1644 | safeResults[rkey] = val;
|
1645 | });
|
1646 | safeResults[key] = args;
|
1647 | hasError = true;
|
1648 | listeners = [];
|
1649 |
|
1650 | callback(err, safeResults);
|
1651 | } else {
|
1652 | results[key] = args;
|
1653 | taskComplete(key);
|
1654 | }
|
1655 | }));
|
1656 |
|
1657 | runningTasks++;
|
1658 | var taskFn = task[task.length - 1];
|
1659 | if (task.length > 1) {
|
1660 | taskFn(results, taskCallback);
|
1661 | } else {
|
1662 | taskFn(taskCallback);
|
1663 | }
|
1664 | }
|
1665 |
|
1666 | function checkForDeadlocks() {
|
1667 | // Kahn's algorithm
|
1668 | // https://en.wikipedia.org/wiki/Topological_sorting#Kahn.27s_algorithm
|
1669 | // http://connalle.blogspot.com/2013/10/topological-sortingkahn-algorithm.html
|
1670 | var currentTask;
|
1671 | var counter = 0;
|
1672 | while (readyToCheck.length) {
|
1673 | currentTask = readyToCheck.pop();
|
1674 | counter++;
|
1675 | arrayEach(getDependents(currentTask), function (dependent) {
|
1676 | if (--uncheckedDependencies[dependent] === 0) {
|
1677 | readyToCheck.push(dependent);
|
1678 | }
|
1679 | });
|
1680 | }
|
1681 |
|
1682 | if (counter !== numTasks) {
|
1683 | throw new Error('async.auto cannot execute tasks due to a recursive dependency');
|
1684 | }
|
1685 | }
|
1686 |
|
1687 | function getDependents(taskName) {
|
1688 | var result = [];
|
1689 | baseForOwn(tasks, function (task, key) {
|
1690 | if (isArray(task) && baseIndexOf(task, taskName, 0) >= 0) {
|
1691 | result.push(key);
|
1692 | }
|
1693 | });
|
1694 | return result;
|
1695 | }
|
1696 | }
|
1697 |
|
1698 | /**
|
1699 | * A specialized version of `_.map` for arrays without support for iteratee
|
1700 | * shorthands.
|
1701 | *
|
1702 | * @private
|
1703 | * @param {Array} [array] The array to iterate over.
|
1704 | * @param {Function} iteratee The function invoked per iteration.
|
1705 | * @returns {Array} Returns the new mapped array.
|
1706 | */
|
1707 | function arrayMap(array, iteratee) {
|
1708 | var index = -1,
|
1709 | length = array ? array.length : 0,
|
1710 | result = Array(length);
|
1711 |
|
1712 | while (++index < length) {
|
1713 | result[index] = iteratee(array[index], index, array);
|
1714 | }
|
1715 | return result;
|
1716 | }
|
1717 |
|
1718 | /**
|
1719 | * Copies the values of `source` to `array`.
|
1720 | *
|
1721 | * @private
|
1722 | * @param {Array} source The array to copy values from.
|
1723 | * @param {Array} [array=[]] The array to copy values to.
|
1724 | * @returns {Array} Returns `array`.
|
1725 | */
|
1726 | function copyArray(source, array) {
|
1727 | var index = -1,
|
1728 | length = source.length;
|
1729 |
|
1730 | array || (array = Array(length));
|
1731 | while (++index < length) {
|
1732 | array[index] = source[index];
|
1733 | }
|
1734 | return array;
|
1735 | }
|
1736 |
|
1737 | /**
|
1738 | * Checks if `value` is a global object.
|
1739 | *
|
1740 | * @private
|
1741 | * @param {*} value The value to check.
|
1742 | * @returns {null|Object} Returns `value` if it's a global object, else `null`.
|
1743 | */
|
1744 | function checkGlobal(value) {
|
1745 | return (value && value.Object === Object) ? value : null;
|
1746 | }
|
1747 |
|
1748 | /** Detect free variable `global` from Node.js. */
|
1749 | var freeGlobal = checkGlobal(typeof global == 'object' && global);
|
1750 |
|
1751 | /** Detect free variable `self`. */
|
1752 | var freeSelf = checkGlobal(typeof self == 'object' && self);
|
1753 |
|
1754 | /** Detect `this` as the global object. */
|
1755 | var thisGlobal = checkGlobal(typeof this == 'object' && this);
|
1756 |
|
1757 | /** Used as a reference to the global object. */
|
1758 | var root = freeGlobal || freeSelf || thisGlobal || Function('return this')();
|
1759 |
|
1760 | /** Built-in value references. */
|
1761 | var Symbol$1 = root.Symbol;
|
1762 |
|
1763 | /** Used as references for various `Number` constants. */
|
1764 | var INFINITY$1 = 1 / 0;
|
1765 |
|
1766 | /** Used to convert symbols to primitives and strings. */
|
1767 | var symbolProto = Symbol$1 ? Symbol$1.prototype : undefined;
|
1768 | var symbolToString = symbolProto ? symbolProto.toString : undefined;
|
1769 | /**
|
1770 | * The base implementation of `_.toString` which doesn't convert nullish
|
1771 | * values to empty strings.
|
1772 | *
|
1773 | * @private
|
1774 | * @param {*} value The value to process.
|
1775 | * @returns {string} Returns the string.
|
1776 | */
|
1777 | function baseToString(value) {
|
1778 | // Exit early for strings to avoid a performance hit in some environments.
|
1779 | if (typeof value == 'string') {
|
1780 | return value;
|
1781 | }
|
1782 | if (isSymbol(value)) {
|
1783 | return symbolToString ? symbolToString.call(value) : '';
|
1784 | }
|
1785 | var result = (value + '');
|
1786 | return (result == '0' && (1 / value) == -INFINITY$1) ? '-0' : result;
|
1787 | }
|
1788 |
|
1789 | /**
|
1790 | * The base implementation of `_.slice` without an iteratee call guard.
|
1791 | *
|
1792 | * @private
|
1793 | * @param {Array} array The array to slice.
|
1794 | * @param {number} [start=0] The start position.
|
1795 | * @param {number} [end=array.length] The end position.
|
1796 | * @returns {Array} Returns the slice of `array`.
|
1797 | */
|
1798 | function baseSlice(array, start, end) {
|
1799 | var index = -1,
|
1800 | length = array.length;
|
1801 |
|
1802 | if (start < 0) {
|
1803 | start = -start > length ? 0 : (length + start);
|
1804 | }
|
1805 | end = end > length ? length : end;
|
1806 | if (end < 0) {
|
1807 | end += length;
|
1808 | }
|
1809 | length = start > end ? 0 : ((end - start) >>> 0);
|
1810 | start >>>= 0;
|
1811 |
|
1812 | var result = Array(length);
|
1813 | while (++index < length) {
|
1814 | result[index] = array[index + start];
|
1815 | }
|
1816 | return result;
|
1817 | }
|
1818 |
|
1819 | /**
|
1820 | * Casts `array` to a slice if it's needed.
|
1821 | *
|
1822 | * @private
|
1823 | * @param {Array} array The array to inspect.
|
1824 | * @param {number} start The start position.
|
1825 | * @param {number} [end=array.length] The end position.
|
1826 | * @returns {Array} Returns the cast slice.
|
1827 | */
|
1828 | function castSlice(array, start, end) {
|
1829 | var length = array.length;
|
1830 | end = end === undefined ? length : end;
|
1831 | return (!start && end >= length) ? array : baseSlice(array, start, end);
|
1832 | }
|
1833 |
|
1834 | /**
|
1835 | * Used by `_.trim` and `_.trimEnd` to get the index of the last string symbol
|
1836 | * that is not found in the character symbols.
|
1837 | *
|
1838 | * @private
|
1839 | * @param {Array} strSymbols The string symbols to inspect.
|
1840 | * @param {Array} chrSymbols The character symbols to find.
|
1841 | * @returns {number} Returns the index of the last unmatched string symbol.
|
1842 | */
|
1843 | function charsEndIndex(strSymbols, chrSymbols) {
|
1844 | var index = strSymbols.length;
|
1845 |
|
1846 | while (index-- && baseIndexOf(chrSymbols, strSymbols[index], 0) > -1) {}
|
1847 | return index;
|
1848 | }
|
1849 |
|
1850 | /**
|
1851 | * Used by `_.trim` and `_.trimStart` to get the index of the first string symbol
|
1852 | * that is not found in the character symbols.
|
1853 | *
|
1854 | * @private
|
1855 | * @param {Array} strSymbols The string symbols to inspect.
|
1856 | * @param {Array} chrSymbols The character symbols to find.
|
1857 | * @returns {number} Returns the index of the first unmatched string symbol.
|
1858 | */
|
1859 | function charsStartIndex(strSymbols, chrSymbols) {
|
1860 | var index = -1,
|
1861 | length = strSymbols.length;
|
1862 |
|
1863 | while (++index < length && baseIndexOf(chrSymbols, strSymbols[index], 0) > -1) {}
|
1864 | return index;
|
1865 | }
|
1866 |
|
1867 | /** Used to compose unicode character classes. */
|
1868 | var rsAstralRange = '\\ud800-\\udfff';
|
1869 | var rsComboMarksRange = '\\u0300-\\u036f\\ufe20-\\ufe23';
|
1870 | var rsComboSymbolsRange = '\\u20d0-\\u20f0';
|
1871 | var rsVarRange = '\\ufe0e\\ufe0f';
|
1872 | var rsAstral = '[' + rsAstralRange + ']';
|
1873 | var rsCombo = '[' + rsComboMarksRange + rsComboSymbolsRange + ']';
|
1874 | var rsFitz = '\\ud83c[\\udffb-\\udfff]';
|
1875 | var rsModifier = '(?:' + rsCombo + '|' + rsFitz + ')';
|
1876 | var rsNonAstral = '[^' + rsAstralRange + ']';
|
1877 | var rsRegional = '(?:\\ud83c[\\udde6-\\uddff]){2}';
|
1878 | var rsSurrPair = '[\\ud800-\\udbff][\\udc00-\\udfff]';
|
1879 | var rsZWJ = '\\u200d';
|
1880 | var reOptMod = rsModifier + '?';
|
1881 | var rsOptVar = '[' + rsVarRange + ']?';
|
1882 | var rsOptJoin = '(?:' + rsZWJ + '(?:' + [rsNonAstral, rsRegional, rsSurrPair].join('|') + ')' + rsOptVar + reOptMod + ')*';
|
1883 | var rsSeq = rsOptVar + reOptMod + rsOptJoin;
|
1884 | var rsSymbol = '(?:' + [rsNonAstral + rsCombo + '?', rsCombo, rsRegional, rsSurrPair, rsAstral].join('|') + ')';
|
1885 | /** Used to match [string symbols](https://mathiasbynens.be/notes/javascript-unicode). */
|
1886 | var reComplexSymbol = RegExp(rsFitz + '(?=' + rsFitz + ')|' + rsSymbol + rsSeq, 'g');
|
1887 |
|
1888 | /**
|
1889 | * Converts `string` to an array.
|
1890 | *
|
1891 | * @private
|
1892 | * @param {string} string The string to convert.
|
1893 | * @returns {Array} Returns the converted array.
|
1894 | */
|
1895 | function stringToArray(string) {
|
1896 | return string.match(reComplexSymbol);
|
1897 | }
|
1898 |
|
1899 | /**
|
1900 | * Converts `value` to a string. An empty string is returned for `null`
|
1901 | * and `undefined` values. The sign of `-0` is preserved.
|
1902 | *
|
1903 | * @static
|
1904 | * @memberOf _
|
1905 | * @since 4.0.0
|
1906 | * @category Lang
|
1907 | * @param {*} value The value to process.
|
1908 | * @returns {string} Returns the string.
|
1909 | * @example
|
1910 | *
|
1911 | * _.toString(null);
|
1912 | * // => ''
|
1913 | *
|
1914 | * _.toString(-0);
|
1915 | * // => '-0'
|
1916 | *
|
1917 | * _.toString([1, 2, 3]);
|
1918 | * // => '1,2,3'
|
1919 | */
|
1920 | function toString(value) {
|
1921 | return value == null ? '' : baseToString(value);
|
1922 | }
|
1923 |
|
1924 | /** Used to match leading and trailing whitespace. */
|
1925 | var reTrim$1 = /^\s+|\s+$/g;
|
1926 |
|
1927 | /**
|
1928 | * Removes leading and trailing whitespace or specified characters from `string`.
|
1929 | *
|
1930 | * @static
|
1931 | * @memberOf _
|
1932 | * @since 3.0.0
|
1933 | * @category String
|
1934 | * @param {string} [string=''] The string to trim.
|
1935 | * @param {string} [chars=whitespace] The characters to trim.
|
1936 | * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
|
1937 | * @returns {string} Returns the trimmed string.
|
1938 | * @example
|
1939 | *
|
1940 | * _.trim(' abc ');
|
1941 | * // => 'abc'
|
1942 | *
|
1943 | * _.trim('-_-abc-_-', '_-');
|
1944 | * // => 'abc'
|
1945 | *
|
1946 | * _.map([' foo ', ' bar '], _.trim);
|
1947 | * // => ['foo', 'bar']
|
1948 | */
|
1949 | function trim(string, chars, guard) {
|
1950 | string = toString(string);
|
1951 | if (string && (guard || chars === undefined)) {
|
1952 | return string.replace(reTrim$1, '');
|
1953 | }
|
1954 | if (!string || !(chars = baseToString(chars))) {
|
1955 | return string;
|
1956 | }
|
1957 | var strSymbols = stringToArray(string),
|
1958 | chrSymbols = stringToArray(chars),
|
1959 | start = charsStartIndex(strSymbols, chrSymbols),
|
1960 | end = charsEndIndex(strSymbols, chrSymbols) + 1;
|
1961 |
|
1962 | return castSlice(strSymbols, start, end).join('');
|
1963 | }
|
1964 |
|
1965 | var FN_ARGS = /^(function)?\s*[^\(]*\(\s*([^\)]*)\)/m;
|
1966 | var FN_ARG_SPLIT = /,/;
|
1967 | var FN_ARG = /(=.+)?(\s*)$/;
|
1968 | var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
|
1969 |
|
1970 | function parseParams(func) {
|
1971 | func = func.toString().replace(STRIP_COMMENTS, '');
|
1972 | func = func.match(FN_ARGS)[2].replace(' ', '');
|
1973 | func = func ? func.split(FN_ARG_SPLIT) : [];
|
1974 | func = func.map(function (arg) {
|
1975 | return trim(arg.replace(FN_ARG, ''));
|
1976 | });
|
1977 | return func;
|
1978 | }
|
1979 |
|
1980 | /**
|
1981 | * A dependency-injected version of the [async.auto]{@link module:ControlFlow.auto} function. Dependent
|
1982 | * tasks are specified as parameters to the function, after the usual callback
|
1983 | * parameter, with the parameter names matching the names of the tasks it
|
1984 | * depends on. This can provide even more readable task graphs which can be
|
1985 | * easier to maintain.
|
1986 | *
|
1987 | * If a final callback is specified, the task results are similarly injected,
|
1988 | * specified as named parameters after the initial error parameter.
|
1989 | *
|
1990 | * The autoInject function is purely syntactic sugar and its semantics are
|
1991 | * otherwise equivalent to [async.auto]{@link module:ControlFlow.auto}.
|
1992 | *
|
1993 | * @name autoInject
|
1994 | * @static
|
1995 | * @memberOf module:ControlFlow
|
1996 | * @method
|
1997 | * @see [async.auto]{@link module:ControlFlow.auto}
|
1998 | * @category Control Flow
|
1999 | * @param {Object} tasks - An object, each of whose properties is a function of
|
2000 | * the form 'func([dependencies...], callback). The object's key of a property
|
2001 | * serves as the name of the task defined by that property, i.e. can be used
|
2002 | * when specifying requirements for other tasks.
|
2003 | * * The `callback` parameter is a `callback(err, result)` which must be called
|
2004 | * when finished, passing an `error` (which can be `null`) and the result of
|
2005 | * the function's execution. The remaining parameters name other tasks on
|
2006 | * which the task is dependent, and the results from those tasks are the
|
2007 | * arguments of those parameters.
|
2008 | * @param {Function} [callback] - An optional callback which is called when all
|
2009 | * the tasks have been completed. It receives the `err` argument if any `tasks`
|
2010 | * pass an error to their callback, and a `results` object with any completed
|
2011 | * task results, similar to `auto`.
|
2012 | * @example
|
2013 | *
|
2014 | * // The example from `auto` can be rewritten as follows:
|
2015 | * async.autoInject({
|
2016 | * get_data: function(callback) {
|
2017 | * // async code to get some data
|
2018 | * callback(null, 'data', 'converted to array');
|
2019 | * },
|
2020 | * make_folder: function(callback) {
|
2021 | * // async code to create a directory to store a file in
|
2022 | * // this is run at the same time as getting the data
|
2023 | * callback(null, 'folder');
|
2024 | * },
|
2025 | * write_file: function(get_data, make_folder, callback) {
|
2026 | * // once there is some data and the directory exists,
|
2027 | * // write the data to a file in the directory
|
2028 | * callback(null, 'filename');
|
2029 | * },
|
2030 | * email_link: function(write_file, callback) {
|
2031 | * // once the file is written let's email a link to it...
|
2032 | * // write_file contains the filename returned by write_file.
|
2033 | * callback(null, {'file':write_file, 'email':'user@example.com'});
|
2034 | * }
|
2035 | * }, function(err, results) {
|
2036 | * console.log('err = ', err);
|
2037 | * console.log('email_link = ', results.email_link);
|
2038 | * });
|
2039 | *
|
2040 | * // If you are using a JS minifier that mangles parameter names, `autoInject`
|
2041 | * // will not work with plain functions, since the parameter names will be
|
2042 | * // collapsed to a single letter identifier. To work around this, you can
|
2043 | * // explicitly specify the names of the parameters your task function needs
|
2044 | * // in an array, similar to Angular.js dependency injection.
|
2045 | *
|
2046 | * // This still has an advantage over plain `auto`, since the results a task
|
2047 | * // depends on are still spread into arguments.
|
2048 | * async.autoInject({
|
2049 | * //...
|
2050 | * write_file: ['get_data', 'make_folder', function(get_data, make_folder, callback) {
|
2051 | * callback(null, 'filename');
|
2052 | * }],
|
2053 | * email_link: ['write_file', function(write_file, callback) {
|
2054 | * callback(null, {'file':write_file, 'email':'user@example.com'});
|
2055 | * }]
|
2056 | * //...
|
2057 | * }, function(err, results) {
|
2058 | * console.log('err = ', err);
|
2059 | * console.log('email_link = ', results.email_link);
|
2060 | * });
|
2061 | */
|
2062 | function autoInject(tasks, callback) {
|
2063 | var newTasks = {};
|
2064 |
|
2065 | baseForOwn(tasks, function (taskFn, key) {
|
2066 | var params;
|
2067 |
|
2068 | if (isArray(taskFn)) {
|
2069 | params = copyArray(taskFn);
|
2070 | taskFn = params.pop();
|
2071 |
|
2072 | newTasks[key] = params.concat(params.length > 0 ? newTask : taskFn);
|
2073 | } else if (taskFn.length === 1) {
|
2074 | // no dependencies, use the function as-is
|
2075 | newTasks[key] = taskFn;
|
2076 | } else {
|
2077 | params = parseParams(taskFn);
|
2078 | if (taskFn.length === 0 && params.length === 0) {
|
2079 | throw new Error("autoInject task functions require explicit parameters.");
|
2080 | }
|
2081 |
|
2082 | params.pop();
|
2083 |
|
2084 | newTasks[key] = params.concat(newTask);
|
2085 | }
|
2086 |
|
2087 | function newTask(results, taskCb) {
|
2088 | var newArgs = arrayMap(params, function (name) {
|
2089 | return results[name];
|
2090 | });
|
2091 | newArgs.push(taskCb);
|
2092 | taskFn.apply(null, newArgs);
|
2093 | }
|
2094 | });
|
2095 |
|
2096 | auto(newTasks, callback);
|
2097 | }
|
2098 |
|
2099 | var hasSetImmediate = typeof setImmediate === 'function' && setImmediate;
|
2100 | var hasNextTick = typeof process === 'object' && typeof process.nextTick === 'function';
|
2101 |
|
2102 | function fallback(fn) {
|
2103 | setTimeout(fn, 0);
|
2104 | }
|
2105 |
|
2106 | function wrap(defer) {
|
2107 | return rest(function (fn, args) {
|
2108 | defer(function () {
|
2109 | fn.apply(null, args);
|
2110 | });
|
2111 | });
|
2112 | }
|
2113 |
|
2114 | var _defer;
|
2115 |
|
2116 | if (hasSetImmediate) {
|
2117 | _defer = setImmediate;
|
2118 | } else if (hasNextTick) {
|
2119 | _defer = process.nextTick;
|
2120 | } else {
|
2121 | _defer = fallback;
|
2122 | }
|
2123 |
|
2124 | var setImmediate$1 = wrap(_defer);
|
2125 |
|
2126 | // Simple doubly linked list (https://en.wikipedia.org/wiki/Doubly_linked_list) implementation
|
2127 | // used for queues. This implementation assumes that the node provided by the user can be modified
|
2128 | // to adjust the next and last properties. We implement only the minimal functionality
|
2129 | // for queue support.
|
2130 | function DLL() {
|
2131 | this.head = this.tail = null;
|
2132 | this.length = 0;
|
2133 | }
|
2134 |
|
2135 | function setInitial(dll, node) {
|
2136 | dll.length = 1;
|
2137 | dll.head = dll.tail = node;
|
2138 | }
|
2139 |
|
2140 | DLL.prototype.removeLink = function (node) {
|
2141 | if (node.prev) node.prev.next = node.next;else this.head = node.next;
|
2142 | if (node.next) node.next.prev = node.prev;else this.tail = node.prev;
|
2143 |
|
2144 | node.prev = node.next = null;
|
2145 | this.length -= 1;
|
2146 | return node;
|
2147 | };
|
2148 |
|
2149 | DLL.prototype.empty = DLL;
|
2150 |
|
2151 | DLL.prototype.insertAfter = function (node, newNode) {
|
2152 | newNode.prev = node;
|
2153 | newNode.next = node.next;
|
2154 | if (node.next) node.next.prev = newNode;else this.tail = newNode;
|
2155 | node.next = newNode;
|
2156 | this.length += 1;
|
2157 | };
|
2158 |
|
2159 | DLL.prototype.insertBefore = function (node, newNode) {
|
2160 | newNode.prev = node.prev;
|
2161 | newNode.next = node;
|
2162 | if (node.prev) node.prev.next = newNode;else this.head = newNode;
|
2163 | node.prev = newNode;
|
2164 | this.length += 1;
|
2165 | };
|
2166 |
|
2167 | DLL.prototype.unshift = function (node) {
|
2168 | if (this.head) this.insertBefore(this.head, node);else setInitial(this, node);
|
2169 | };
|
2170 |
|
2171 | DLL.prototype.push = function (node) {
|
2172 | if (this.tail) this.insertAfter(this.tail, node);else setInitial(this, node);
|
2173 | };
|
2174 |
|
2175 | DLL.prototype.shift = function () {
|
2176 | return this.head && this.removeLink(this.head);
|
2177 | };
|
2178 |
|
2179 | DLL.prototype.pop = function () {
|
2180 | return this.tail && this.removeLink(this.tail);
|
2181 | };
|
2182 |
|
2183 | function queue(worker, concurrency, payload) {
|
2184 | if (concurrency == null) {
|
2185 | concurrency = 1;
|
2186 | } else if (concurrency === 0) {
|
2187 | throw new Error('Concurrency must not be zero');
|
2188 | }
|
2189 |
|
2190 | function _insert(data, insertAtFront, callback) {
|
2191 | if (callback != null && typeof callback !== 'function') {
|
2192 | throw new Error('task callback must be a function');
|
2193 | }
|
2194 | q.started = true;
|
2195 | if (!isArray(data)) {
|
2196 | data = [data];
|
2197 | }
|
2198 | if (data.length === 0 && q.idle()) {
|
2199 | // call drain immediately if there are no tasks
|
2200 | return setImmediate$1(function () {
|
2201 | q.drain();
|
2202 | });
|
2203 | }
|
2204 | arrayEach(data, function (task) {
|
2205 | var item = {
|
2206 | data: task,
|
2207 | callback: callback || noop
|
2208 | };
|
2209 |
|
2210 | if (insertAtFront) {
|
2211 | q._tasks.unshift(item);
|
2212 | } else {
|
2213 | q._tasks.push(item);
|
2214 | }
|
2215 | });
|
2216 | setImmediate$1(q.process);
|
2217 | }
|
2218 |
|
2219 | function _next(tasks) {
|
2220 | return rest(function (args) {
|
2221 | workers -= 1;
|
2222 |
|
2223 | arrayEach(tasks, function (task) {
|
2224 | arrayEach(workersList, function (worker, index) {
|
2225 | if (worker === task) {
|
2226 | workersList.splice(index, 1);
|
2227 | return false;
|
2228 | }
|
2229 | });
|
2230 |
|
2231 | task.callback.apply(task, args);
|
2232 |
|
2233 | if (args[0] != null) {
|
2234 | q.error(args[0], task.data);
|
2235 | }
|
2236 | });
|
2237 |
|
2238 | if (workers <= q.concurrency - q.buffer) {
|
2239 | q.unsaturated();
|
2240 | }
|
2241 |
|
2242 | if (q.idle()) {
|
2243 | q.drain();
|
2244 | }
|
2245 | q.process();
|
2246 | });
|
2247 | }
|
2248 |
|
2249 | var workers = 0;
|
2250 | var workersList = [];
|
2251 | var q = {
|
2252 | _tasks: new DLL(),
|
2253 | concurrency: concurrency,
|
2254 | payload: payload,
|
2255 | saturated: noop,
|
2256 | unsaturated: noop,
|
2257 | buffer: concurrency / 4,
|
2258 | empty: noop,
|
2259 | drain: noop,
|
2260 | error: noop,
|
2261 | started: false,
|
2262 | paused: false,
|
2263 | push: function (data, callback) {
|
2264 | _insert(data, false, callback);
|
2265 | },
|
2266 | kill: function () {
|
2267 | q.drain = noop;
|
2268 | q._tasks.empty();
|
2269 | },
|
2270 | unshift: function (data, callback) {
|
2271 | _insert(data, true, callback);
|
2272 | },
|
2273 | process: function () {
|
2274 | while (!q.paused && workers < q.concurrency && q._tasks.length) {
|
2275 | var tasks = [],
|
2276 | data = [];
|
2277 | var l = q._tasks.length;
|
2278 | if (q.payload) l = Math.min(l, q.payload);
|
2279 | for (var i = 0; i < l; i++) {
|
2280 | var node = q._tasks.shift();
|
2281 | tasks.push(node);
|
2282 | data.push(node.data);
|
2283 | }
|
2284 |
|
2285 | if (q._tasks.length === 0) {
|
2286 | q.empty();
|
2287 | }
|
2288 | workers += 1;
|
2289 | workersList.push(tasks[0]);
|
2290 |
|
2291 | if (workers === q.concurrency) {
|
2292 | q.saturated();
|
2293 | }
|
2294 |
|
2295 | var cb = onlyOnce(_next(tasks));
|
2296 | worker(data, cb);
|
2297 | }
|
2298 | },
|
2299 | length: function () {
|
2300 | return q._tasks.length;
|
2301 | },
|
2302 | running: function () {
|
2303 | return workers;
|
2304 | },
|
2305 | workersList: function () {
|
2306 | return workersList;
|
2307 | },
|
2308 | idle: function () {
|
2309 | return q._tasks.length + workers === 0;
|
2310 | },
|
2311 | pause: function () {
|
2312 | q.paused = true;
|
2313 | },
|
2314 | resume: function () {
|
2315 | if (q.paused === false) {
|
2316 | return;
|
2317 | }
|
2318 | q.paused = false;
|
2319 | var resumeCount = Math.min(q.concurrency, q._tasks.length);
|
2320 | // Need to call q.process once per concurrent
|
2321 | // worker to preserve full concurrency after pause
|
2322 | for (var w = 1; w <= resumeCount; w++) {
|
2323 | setImmediate$1(q.process);
|
2324 | }
|
2325 | }
|
2326 | };
|
2327 | return q;
|
2328 | }
|
2329 |
|
2330 | /**
|
2331 | * A cargo of tasks for the worker function to complete. Cargo inherits all of
|
2332 | * the same methods and event callbacks as [`queue`]{@link module:ControlFlow.queue}.
|
2333 | * @typedef {Object} CargoObject
|
2334 | * @memberOf module:ControlFlow
|
2335 | * @property {Function} length - A function returning the number of items
|
2336 | * waiting to be processed. Invoke like `cargo.length()`.
|
2337 | * @property {number} payload - An `integer` for determining how many tasks
|
2338 | * should be process per round. This property can be changed after a `cargo` is
|
2339 | * created to alter the payload on-the-fly.
|
2340 | * @property {Function} push - Adds `task` to the `queue`. The callback is
|
2341 | * called once the `worker` has finished processing the task. Instead of a
|
2342 | * single task, an array of `tasks` can be submitted. The respective callback is
|
2343 | * used for every task in the list. Invoke like `cargo.push(task, [callback])`.
|
2344 | * @property {Function} saturated - A callback that is called when the
|
2345 | * `queue.length()` hits the concurrency and further tasks will be queued.
|
2346 | * @property {Function} empty - A callback that is called when the last item
|
2347 | * from the `queue` is given to a `worker`.
|
2348 | * @property {Function} drain - A callback that is called when the last item
|
2349 | * from the `queue` has returned from the `worker`.
|
2350 | * @property {Function} idle - a function returning false if there are items
|
2351 | * waiting or being processed, or true if not. Invoke like `cargo.idle()`.
|
2352 | * @property {Function} pause - a function that pauses the processing of tasks
|
2353 | * until `resume()` is called. Invoke like `cargo.pause()`.
|
2354 | * @property {Function} resume - a function that resumes the processing of
|
2355 | * queued tasks when the queue is paused. Invoke like `cargo.resume()`.
|
2356 | * @property {Function} kill - a function that removes the `drain` callback and
|
2357 | * empties remaining tasks from the queue forcing it to go idle. Invoke like `cargo.kill()`.
|
2358 | */
|
2359 |
|
2360 | /**
|
2361 | * Creates a `cargo` object with the specified payload. Tasks added to the
|
2362 | * cargo will be processed altogether (up to the `payload` limit). If the
|
2363 | * `worker` is in progress, the task is queued until it becomes available. Once
|
2364 | * the `worker` has completed some tasks, each callback of those tasks is
|
2365 | * called. Check out [these](https://camo.githubusercontent.com/6bbd36f4cf5b35a0f11a96dcd2e97711ffc2fb37/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f313637363837312f36383130382f62626330636662302d356632392d313165322d393734662d3333393763363464633835382e676966) [animations](https://camo.githubusercontent.com/f4810e00e1c5f5f8addbe3e9f49064fd5d102699/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f313637363837312f36383130312f38346339323036362d356632392d313165322d383134662d3964336430323431336266642e676966)
|
2366 | * for how `cargo` and `queue` work.
|
2367 | *
|
2368 | * While [`queue`]{@link module:ControlFlow.queue} passes only one task to one of a group of workers
|
2369 | * at a time, cargo passes an array of tasks to a single worker, repeating
|
2370 | * when the worker is finished.
|
2371 | *
|
2372 | * @name cargo
|
2373 | * @static
|
2374 | * @memberOf module:ControlFlow
|
2375 | * @method
|
2376 | * @see [async.queue]{@link module:ControlFlow.queue}
|
2377 | * @category Control Flow
|
2378 | * @param {Function} worker - An asynchronous function for processing an array
|
2379 | * of queued tasks, which must call its `callback(err)` argument when finished,
|
2380 | * with an optional `err` argument. Invoked with `(tasks, callback)`.
|
2381 | * @param {number} [payload=Infinity] - An optional `integer` for determining
|
2382 | * how many tasks should be processed per round; if omitted, the default is
|
2383 | * unlimited.
|
2384 | * @returns {module:ControlFlow.CargoObject} A cargo object to manage the tasks. Callbacks can
|
2385 | * attached as certain properties to listen for specific events during the
|
2386 | * lifecycle of the cargo and inner queue.
|
2387 | * @example
|
2388 | *
|
2389 | * // create a cargo object with payload 2
|
2390 | * var cargo = async.cargo(function(tasks, callback) {
|
2391 | * for (var i=0; i<tasks.length; i++) {
|
2392 | * console.log('hello ' + tasks[i].name);
|
2393 | * }
|
2394 | * callback();
|
2395 | * }, 2);
|
2396 | *
|
2397 | * // add some items
|
2398 | * cargo.push({name: 'foo'}, function(err) {
|
2399 | * console.log('finished processing foo');
|
2400 | * });
|
2401 | * cargo.push({name: 'bar'}, function(err) {
|
2402 | * console.log('finished processing bar');
|
2403 | * });
|
2404 | * cargo.push({name: 'baz'}, function(err) {
|
2405 | * console.log('finished processing baz');
|
2406 | * });
|
2407 | */
|
2408 | function cargo(worker, payload) {
|
2409 | return queue(worker, 1, payload);
|
2410 | }
|
2411 |
|
2412 | /**
|
2413 | * The same as [`eachOf`]{@link module:Collections.eachOf} but runs only a single async operation at a time.
|
2414 | *
|
2415 | * @name eachOfSeries
|
2416 | * @static
|
2417 | * @memberOf module:Collections
|
2418 | * @method
|
2419 | * @see [async.eachOf]{@link module:Collections.eachOf}
|
2420 | * @alias forEachOfSeries
|
2421 | * @category Collection
|
2422 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
2423 | * @param {Function} iteratee - A function to apply to each item in `coll`. The
|
2424 | * `key` is the item's key, or index in the case of an array. The iteratee is
|
2425 | * passed a `callback(err)` which must be called once it has completed. If no
|
2426 | * error has occurred, the callback should be run without arguments or with an
|
2427 | * explicit `null` argument. Invoked with (item, key, callback).
|
2428 | * @param {Function} [callback] - A callback which is called when all `iteratee`
|
2429 | * functions have finished, or an error occurs. Invoked with (err).
|
2430 | */
|
2431 | var eachOfSeries = doLimit(eachOfLimit, 1);
|
2432 |
|
2433 | /**
|
2434 | * Reduces `coll` into a single value using an async `iteratee` to return each
|
2435 | * successive step. `memo` is the initial state of the reduction. This function
|
2436 | * only operates in series.
|
2437 | *
|
2438 | * For performance reasons, it may make sense to split a call to this function
|
2439 | * into a parallel map, and then use the normal `Array.prototype.reduce` on the
|
2440 | * results. This function is for situations where each step in the reduction
|
2441 | * needs to be async; if you can get the data before reducing it, then it's
|
2442 | * probably a good idea to do so.
|
2443 | *
|
2444 | * @name reduce
|
2445 | * @static
|
2446 | * @memberOf module:Collections
|
2447 | * @method
|
2448 | * @alias inject
|
2449 | * @alias foldl
|
2450 | * @category Collection
|
2451 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
2452 | * @param {*} memo - The initial state of the reduction.
|
2453 | * @param {Function} iteratee - A function applied to each item in the
|
2454 | * array to produce the next step in the reduction. The `iteratee` is passed a
|
2455 | * `callback(err, reduction)` which accepts an optional error as its first
|
2456 | * argument, and the state of the reduction as the second. If an error is
|
2457 | * passed to the callback, the reduction is stopped and the main `callback` is
|
2458 | * immediately called with the error. Invoked with (memo, item, callback).
|
2459 | * @param {Function} [callback] - A callback which is called after all the
|
2460 | * `iteratee` functions have finished. Result is the reduced value. Invoked with
|
2461 | * (err, result).
|
2462 | * @example
|
2463 | *
|
2464 | * async.reduce([1,2,3], 0, function(memo, item, callback) {
|
2465 | * // pointless async:
|
2466 | * process.nextTick(function() {
|
2467 | * callback(null, memo + item)
|
2468 | * });
|
2469 | * }, function(err, result) {
|
2470 | * // result is now equal to the last value of memo, which is 6
|
2471 | * });
|
2472 | */
|
2473 | function reduce(coll, memo, iteratee, callback) {
|
2474 | callback = once(callback || noop);
|
2475 | eachOfSeries(coll, function (x, i, callback) {
|
2476 | iteratee(memo, x, function (err, v) {
|
2477 | memo = v;
|
2478 | callback(err);
|
2479 | });
|
2480 | }, function (err) {
|
2481 | callback(err, memo);
|
2482 | });
|
2483 | }
|
2484 |
|
2485 | /**
|
2486 | * Version of the compose function that is more natural to read. Each function
|
2487 | * consumes the return value of the previous function. It is the equivalent of
|
2488 | * [compose]{@link module:ControlFlow.compose} with the arguments reversed.
|
2489 | *
|
2490 | * Each function is executed with the `this` binding of the composed function.
|
2491 | *
|
2492 | * @name seq
|
2493 | * @static
|
2494 | * @memberOf module:ControlFlow
|
2495 | * @method
|
2496 | * @see [async.compose]{@link module:ControlFlow.compose}
|
2497 | * @category Control Flow
|
2498 | * @param {...Function} functions - the asynchronous functions to compose
|
2499 | * @returns {Function} a function that composes the `functions` in order
|
2500 | * @example
|
2501 | *
|
2502 | * // Requires lodash (or underscore), express3 and dresende's orm2.
|
2503 | * // Part of an app, that fetches cats of the logged user.
|
2504 | * // This example uses `seq` function to avoid overnesting and error
|
2505 | * // handling clutter.
|
2506 | * app.get('/cats', function(request, response) {
|
2507 | * var User = request.models.User;
|
2508 | * async.seq(
|
2509 | * _.bind(User.get, User), // 'User.get' has signature (id, callback(err, data))
|
2510 | * function(user, fn) {
|
2511 | * user.getCats(fn); // 'getCats' has signature (callback(err, data))
|
2512 | * }
|
2513 | * )(req.session.user_id, function (err, cats) {
|
2514 | * if (err) {
|
2515 | * console.error(err);
|
2516 | * response.json({ status: 'error', message: err.message });
|
2517 | * } else {
|
2518 | * response.json({ status: 'ok', message: 'Cats found', data: cats });
|
2519 | * }
|
2520 | * });
|
2521 | * });
|
2522 | */
|
2523 | var seq = rest(function seq(functions) {
|
2524 | return rest(function (args) {
|
2525 | var that = this;
|
2526 |
|
2527 | var cb = args[args.length - 1];
|
2528 | if (typeof cb == 'function') {
|
2529 | args.pop();
|
2530 | } else {
|
2531 | cb = noop;
|
2532 | }
|
2533 |
|
2534 | reduce(functions, args, function (newargs, fn, cb) {
|
2535 | fn.apply(that, newargs.concat([rest(function (err, nextargs) {
|
2536 | cb(err, nextargs);
|
2537 | })]));
|
2538 | }, function (err, results) {
|
2539 | cb.apply(that, [err].concat(results));
|
2540 | });
|
2541 | });
|
2542 | });
|
2543 |
|
2544 | /**
|
2545 | * Creates a function which is a composition of the passed asynchronous
|
2546 | * functions. Each function consumes the return value of the function that
|
2547 | * follows. Composing functions `f()`, `g()`, and `h()` would produce the result
|
2548 | * of `f(g(h()))`, only this version uses callbacks to obtain the return values.
|
2549 | *
|
2550 | * Each function is executed with the `this` binding of the composed function.
|
2551 | *
|
2552 | * @name compose
|
2553 | * @static
|
2554 | * @memberOf module:ControlFlow
|
2555 | * @method
|
2556 | * @category Control Flow
|
2557 | * @param {...Function} functions - the asynchronous functions to compose
|
2558 | * @returns {Function} an asynchronous function that is the composed
|
2559 | * asynchronous `functions`
|
2560 | * @example
|
2561 | *
|
2562 | * function add1(n, callback) {
|
2563 | * setTimeout(function () {
|
2564 | * callback(null, n + 1);
|
2565 | * }, 10);
|
2566 | * }
|
2567 | *
|
2568 | * function mul3(n, callback) {
|
2569 | * setTimeout(function () {
|
2570 | * callback(null, n * 3);
|
2571 | * }, 10);
|
2572 | * }
|
2573 | *
|
2574 | * var add1mul3 = async.compose(mul3, add1);
|
2575 | * add1mul3(4, function (err, result) {
|
2576 | * // result now equals 15
|
2577 | * });
|
2578 | */
|
2579 | var compose = rest(function (args) {
|
2580 | return seq.apply(null, args.reverse());
|
2581 | });
|
2582 |
|
2583 | function concat$1(eachfn, arr, fn, callback) {
|
2584 | var result = [];
|
2585 | eachfn(arr, function (x, index, cb) {
|
2586 | fn(x, function (err, y) {
|
2587 | result = result.concat(y || []);
|
2588 | cb(err);
|
2589 | });
|
2590 | }, function (err) {
|
2591 | callback(err, result);
|
2592 | });
|
2593 | }
|
2594 |
|
2595 | /**
|
2596 | * Applies `iteratee` to each item in `coll`, concatenating the results. Returns
|
2597 | * the concatenated list. The `iteratee`s are called in parallel, and the
|
2598 | * results are concatenated as they return. There is no guarantee that the
|
2599 | * results array will be returned in the original order of `coll` passed to the
|
2600 | * `iteratee` function.
|
2601 | *
|
2602 | * @name concat
|
2603 | * @static
|
2604 | * @memberOf module:Collections
|
2605 | * @method
|
2606 | * @category Collection
|
2607 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
2608 | * @param {Function} iteratee - A function to apply to each item in `coll`.
|
2609 | * The iteratee is passed a `callback(err, results)` which must be called once
|
2610 | * it has completed with an error (which can be `null`) and an array of results.
|
2611 | * Invoked with (item, callback).
|
2612 | * @param {Function} [callback(err)] - A callback which is called after all the
|
2613 | * `iteratee` functions have finished, or an error occurs. Results is an array
|
2614 | * containing the concatenated results of the `iteratee` function. Invoked with
|
2615 | * (err, results).
|
2616 | * @example
|
2617 | *
|
2618 | * async.concat(['dir1','dir2','dir3'], fs.readdir, function(err, files) {
|
2619 | * // files is now a list of filenames that exist in the 3 directories
|
2620 | * });
|
2621 | */
|
2622 | var concat = doParallel(concat$1);
|
2623 |
|
2624 | function doSeries(fn) {
|
2625 | return function (obj, iteratee, callback) {
|
2626 | return fn(eachOfSeries, obj, iteratee, callback);
|
2627 | };
|
2628 | }
|
2629 |
|
2630 | /**
|
2631 | * The same as [`concat`]{@link module:Collections.concat} but runs only a single async operation at a time.
|
2632 | *
|
2633 | * @name concatSeries
|
2634 | * @static
|
2635 | * @memberOf module:Collections
|
2636 | * @method
|
2637 | * @see [async.concat]{@link module:Collections.concat}
|
2638 | * @category Collection
|
2639 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
2640 | * @param {Function} iteratee - A function to apply to each item in `coll`.
|
2641 | * The iteratee is passed a `callback(err, results)` which must be called once
|
2642 | * it has completed with an error (which can be `null`) and an array of results.
|
2643 | * Invoked with (item, callback).
|
2644 | * @param {Function} [callback(err)] - A callback which is called after all the
|
2645 | * `iteratee` functions have finished, or an error occurs. Results is an array
|
2646 | * containing the concatenated results of the `iteratee` function. Invoked with
|
2647 | * (err, results).
|
2648 | */
|
2649 | var concatSeries = doSeries(concat$1);
|
2650 |
|
2651 | /**
|
2652 | * Returns a function that when called, calls-back with the values provided.
|
2653 | * Useful as the first function in a [`waterfall`]{@link module:ControlFlow.waterfall}, or for plugging values in to
|
2654 | * [`auto`]{@link module:ControlFlow.auto}.
|
2655 | *
|
2656 | * @name constant
|
2657 | * @static
|
2658 | * @memberOf module:Utils
|
2659 | * @method
|
2660 | * @category Util
|
2661 | * @param {...*} arguments... - Any number of arguments to automatically invoke
|
2662 | * callback with.
|
2663 | * @returns {Function} Returns a function that when invoked, automatically
|
2664 | * invokes the callback with the previous given arguments.
|
2665 | * @example
|
2666 | *
|
2667 | * async.waterfall([
|
2668 | * async.constant(42),
|
2669 | * function (value, next) {
|
2670 | * // value === 42
|
2671 | * },
|
2672 | * //...
|
2673 | * ], callback);
|
2674 | *
|
2675 | * async.waterfall([
|
2676 | * async.constant(filename, "utf8"),
|
2677 | * fs.readFile,
|
2678 | * function (fileData, next) {
|
2679 | * //...
|
2680 | * }
|
2681 | * //...
|
2682 | * ], callback);
|
2683 | *
|
2684 | * async.auto({
|
2685 | * hostname: async.constant("https://server.net/"),
|
2686 | * port: findFreePort,
|
2687 | * launchServer: ["hostname", "port", function (options, cb) {
|
2688 | * startServer(options, cb);
|
2689 | * }],
|
2690 | * //...
|
2691 | * }, callback);
|
2692 | */
|
2693 | var constant = rest(function (values) {
|
2694 | var args = [null].concat(values);
|
2695 | return initialParams(function (ignoredArgs, callback) {
|
2696 | return callback.apply(this, args);
|
2697 | });
|
2698 | });
|
2699 |
|
2700 | /**
|
2701 | * This method returns the first argument given to it.
|
2702 | *
|
2703 | * @static
|
2704 | * @since 0.1.0
|
2705 | * @memberOf _
|
2706 | * @category Util
|
2707 | * @param {*} value Any value.
|
2708 | * @returns {*} Returns `value`.
|
2709 | * @example
|
2710 | *
|
2711 | * var object = { 'user': 'fred' };
|
2712 | *
|
2713 | * console.log(_.identity(object) === object);
|
2714 | * // => true
|
2715 | */
|
2716 | function identity(value) {
|
2717 | return value;
|
2718 | }
|
2719 |
|
2720 | function _createTester(eachfn, check, getResult) {
|
2721 | return function (arr, limit, iteratee, cb) {
|
2722 | function done(err) {
|
2723 | if (cb) {
|
2724 | if (err) {
|
2725 | cb(err);
|
2726 | } else {
|
2727 | cb(null, getResult(false));
|
2728 | }
|
2729 | }
|
2730 | }
|
2731 | function wrappedIteratee(x, _, callback) {
|
2732 | if (!cb) return callback();
|
2733 | iteratee(x, function (err, v) {
|
2734 | if (cb) {
|
2735 | if (err) {
|
2736 | cb(err);
|
2737 | cb = iteratee = false;
|
2738 | } else if (check(v)) {
|
2739 | cb(null, getResult(true, x));
|
2740 | cb = iteratee = false;
|
2741 | }
|
2742 | }
|
2743 | callback();
|
2744 | });
|
2745 | }
|
2746 | if (arguments.length > 3) {
|
2747 | cb = cb || noop;
|
2748 | eachfn(arr, limit, wrappedIteratee, done);
|
2749 | } else {
|
2750 | cb = iteratee;
|
2751 | cb = cb || noop;
|
2752 | iteratee = limit;
|
2753 | eachfn(arr, wrappedIteratee, done);
|
2754 | }
|
2755 | };
|
2756 | }
|
2757 |
|
2758 | function _findGetResult(v, x) {
|
2759 | return x;
|
2760 | }
|
2761 |
|
2762 | /**
|
2763 | * Returns the first value in `coll` that passes an async truth test. The
|
2764 | * `iteratee` is applied in parallel, meaning the first iteratee to return
|
2765 | * `true` will fire the detect `callback` with that result. That means the
|
2766 | * result might not be the first item in the original `coll` (in terms of order)
|
2767 | * that passes the test.
|
2768 |
|
2769 | * If order within the original `coll` is important, then look at
|
2770 | * [`detectSeries`]{@link module:Collections.detectSeries}.
|
2771 | *
|
2772 | * @name detect
|
2773 | * @static
|
2774 | * @memberOf module:Collections
|
2775 | * @method
|
2776 | * @alias find
|
2777 | * @category Collections
|
2778 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
2779 | * @param {Function} iteratee - A truth test to apply to each item in `coll`.
|
2780 | * The iteratee is passed a `callback(err, truthValue)` which must be called
|
2781 | * with a boolean argument once it has completed. Invoked with (item, callback).
|
2782 | * @param {Function} [callback] - A callback which is called as soon as any
|
2783 | * iteratee returns `true`, or after all the `iteratee` functions have finished.
|
2784 | * Result will be the first item in the array that passes the truth test
|
2785 | * (iteratee) or the value `undefined` if none passed. Invoked with
|
2786 | * (err, result).
|
2787 | * @example
|
2788 | *
|
2789 | * async.detect(['file1','file2','file3'], function(filePath, callback) {
|
2790 | * fs.access(filePath, function(err) {
|
2791 | * callback(null, !err)
|
2792 | * });
|
2793 | * }, function(err, result) {
|
2794 | * // result now equals the first file in the list that exists
|
2795 | * });
|
2796 | */
|
2797 | var detect = _createTester(eachOf, identity, _findGetResult);
|
2798 |
|
2799 | /**
|
2800 | * The same as [`detect`]{@link module:Collections.detect} but runs a maximum of `limit` async operations at a
|
2801 | * time.
|
2802 | *
|
2803 | * @name detectLimit
|
2804 | * @static
|
2805 | * @memberOf module:Collections
|
2806 | * @method
|
2807 | * @see [async.detect]{@link module:Collections.detect}
|
2808 | * @alias findLimit
|
2809 | * @category Collections
|
2810 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
2811 | * @param {number} limit - The maximum number of async operations at a time.
|
2812 | * @param {Function} iteratee - A truth test to apply to each item in `coll`.
|
2813 | * The iteratee is passed a `callback(err, truthValue)` which must be called
|
2814 | * with a boolean argument once it has completed. Invoked with (item, callback).
|
2815 | * @param {Function} [callback] - A callback which is called as soon as any
|
2816 | * iteratee returns `true`, or after all the `iteratee` functions have finished.
|
2817 | * Result will be the first item in the array that passes the truth test
|
2818 | * (iteratee) or the value `undefined` if none passed. Invoked with
|
2819 | * (err, result).
|
2820 | */
|
2821 | var detectLimit = _createTester(eachOfLimit, identity, _findGetResult);
|
2822 |
|
2823 | /**
|
2824 | * The same as [`detect`]{@link module:Collections.detect} but runs only a single async operation at a time.
|
2825 | *
|
2826 | * @name detectSeries
|
2827 | * @static
|
2828 | * @memberOf module:Collections
|
2829 | * @method
|
2830 | * @see [async.detect]{@link module:Collections.detect}
|
2831 | * @alias findSeries
|
2832 | * @category Collections
|
2833 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
2834 | * @param {Function} iteratee - A truth test to apply to each item in `coll`.
|
2835 | * The iteratee is passed a `callback(err, truthValue)` which must be called
|
2836 | * with a boolean argument once it has completed. Invoked with (item, callback).
|
2837 | * @param {Function} [callback] - A callback which is called as soon as any
|
2838 | * iteratee returns `true`, or after all the `iteratee` functions have finished.
|
2839 | * Result will be the first item in the array that passes the truth test
|
2840 | * (iteratee) or the value `undefined` if none passed. Invoked with
|
2841 | * (err, result).
|
2842 | */
|
2843 | var detectSeries = _createTester(eachOfSeries, identity, _findGetResult);
|
2844 |
|
2845 | function consoleFunc(name) {
|
2846 | return rest(function (fn, args) {
|
2847 | fn.apply(null, args.concat([rest(function (err, args) {
|
2848 | if (typeof console === 'object') {
|
2849 | if (err) {
|
2850 | if (console.error) {
|
2851 | console.error(err);
|
2852 | }
|
2853 | } else if (console[name]) {
|
2854 | arrayEach(args, function (x) {
|
2855 | console[name](x);
|
2856 | });
|
2857 | }
|
2858 | }
|
2859 | })]));
|
2860 | });
|
2861 | }
|
2862 |
|
2863 | /**
|
2864 | * Logs the result of an `async` function to the `console` using `console.dir`
|
2865 | * to display the properties of the resulting object. Only works in Node.js or
|
2866 | * in browsers that support `console.dir` and `console.error` (such as FF and
|
2867 | * Chrome). If multiple arguments are returned from the async function,
|
2868 | * `console.dir` is called on each argument in order.
|
2869 | *
|
2870 | * @name dir
|
2871 | * @static
|
2872 | * @memberOf module:Utils
|
2873 | * @method
|
2874 | * @category Util
|
2875 | * @param {Function} function - The function you want to eventually apply all
|
2876 | * arguments to.
|
2877 | * @param {...*} arguments... - Any number of arguments to apply to the function.
|
2878 | * @example
|
2879 | *
|
2880 | * // in a module
|
2881 | * var hello = function(name, callback) {
|
2882 | * setTimeout(function() {
|
2883 | * callback(null, {hello: name});
|
2884 | * }, 1000);
|
2885 | * };
|
2886 | *
|
2887 | * // in the node repl
|
2888 | * node> async.dir(hello, 'world');
|
2889 | * {hello: 'world'}
|
2890 | */
|
2891 | var dir = consoleFunc('dir');
|
2892 |
|
2893 | /**
|
2894 | * The post-check version of [`during`]{@link module:ControlFlow.during}. To reflect the difference in
|
2895 | * the order of operations, the arguments `test` and `fn` are switched.
|
2896 | *
|
2897 | * Also a version of [`doWhilst`]{@link module:ControlFlow.doWhilst} with asynchronous `test` function.
|
2898 | * @name doDuring
|
2899 | * @static
|
2900 | * @memberOf module:ControlFlow
|
2901 | * @method
|
2902 | * @see [async.during]{@link module:ControlFlow.during}
|
2903 | * @category Control Flow
|
2904 | * @param {Function} fn - A function which is called each time `test` passes.
|
2905 | * The function is passed a `callback(err)`, which must be called once it has
|
2906 | * completed with an optional `err` argument. Invoked with (callback).
|
2907 | * @param {Function} test - asynchronous truth test to perform before each
|
2908 | * execution of `fn`. Invoked with (...args, callback), where `...args` are the
|
2909 | * non-error args from the previous callback of `fn`.
|
2910 | * @param {Function} [callback] - A callback which is called after the test
|
2911 | * function has failed and repeated execution of `fn` has stopped. `callback`
|
2912 | * will be passed an error if one occured, otherwise `null`.
|
2913 | */
|
2914 | function doDuring(fn, test, callback) {
|
2915 | callback = onlyOnce(callback || noop);
|
2916 |
|
2917 | var next = rest(function (err, args) {
|
2918 | if (err) return callback(err);
|
2919 | args.push(check);
|
2920 | test.apply(this, args);
|
2921 | });
|
2922 |
|
2923 | function check(err, truth) {
|
2924 | if (err) return callback(err);
|
2925 | if (!truth) return callback(null);
|
2926 | fn(next);
|
2927 | }
|
2928 |
|
2929 | check(null, true);
|
2930 | }
|
2931 |
|
2932 | /**
|
2933 | * The post-check version of [`whilst`]{@link module:ControlFlow.whilst}. To reflect the difference in
|
2934 | * the order of operations, the arguments `test` and `iteratee` are switched.
|
2935 | *
|
2936 | * `doWhilst` is to `whilst` as `do while` is to `while` in plain JavaScript.
|
2937 | *
|
2938 | * @name doWhilst
|
2939 | * @static
|
2940 | * @memberOf module:ControlFlow
|
2941 | * @method
|
2942 | * @see [async.whilst]{@link module:ControlFlow.whilst}
|
2943 | * @category Control Flow
|
2944 | * @param {Function} iteratee - A function which is called each time `test`
|
2945 | * passes. The function is passed a `callback(err)`, which must be called once
|
2946 | * it has completed with an optional `err` argument. Invoked with (callback).
|
2947 | * @param {Function} test - synchronous truth test to perform after each
|
2948 | * execution of `iteratee`. Invoked with Invoked with the non-error callback
|
2949 | * results of `iteratee`.
|
2950 | * @param {Function} [callback] - A callback which is called after the test
|
2951 | * function has failed and repeated execution of `iteratee` has stopped.
|
2952 | * `callback` will be passed an error and any arguments passed to the final
|
2953 | * `iteratee`'s callback. Invoked with (err, [results]);
|
2954 | */
|
2955 | function doWhilst(iteratee, test, callback) {
|
2956 | callback = onlyOnce(callback || noop);
|
2957 | var next = rest(function (err, args) {
|
2958 | if (err) return callback(err);
|
2959 | if (test.apply(this, args)) return iteratee(next);
|
2960 | callback.apply(null, [null].concat(args));
|
2961 | });
|
2962 | iteratee(next);
|
2963 | }
|
2964 |
|
2965 | /**
|
2966 | * Like ['doWhilst']{@link module:ControlFlow.doWhilst}, except the `test` is inverted. Note the
|
2967 | * argument ordering differs from `until`.
|
2968 | *
|
2969 | * @name doUntil
|
2970 | * @static
|
2971 | * @memberOf module:ControlFlow
|
2972 | * @method
|
2973 | * @see [async.doWhilst]{@link module:ControlFlow.doWhilst}
|
2974 | * @category Control Flow
|
2975 | * @param {Function} fn - A function which is called each time `test` fails.
|
2976 | * The function is passed a `callback(err)`, which must be called once it has
|
2977 | * completed with an optional `err` argument. Invoked with (callback).
|
2978 | * @param {Function} test - synchronous truth test to perform after each
|
2979 | * execution of `fn`. Invoked with the non-error callback results of `fn`.
|
2980 | * @param {Function} [callback] - A callback which is called after the test
|
2981 | * function has passed and repeated execution of `fn` has stopped. `callback`
|
2982 | * will be passed an error and any arguments passed to the final `fn`'s
|
2983 | * callback. Invoked with (err, [results]);
|
2984 | */
|
2985 | function doUntil(fn, test, callback) {
|
2986 | doWhilst(fn, function () {
|
2987 | return !test.apply(this, arguments);
|
2988 | }, callback);
|
2989 | }
|
2990 |
|
2991 | /**
|
2992 | * Like [`whilst`]{@link module:ControlFlow.whilst}, except the `test` is an asynchronous function that
|
2993 | * is passed a callback in the form of `function (err, truth)`. If error is
|
2994 | * passed to `test` or `fn`, the main callback is immediately called with the
|
2995 | * value of the error.
|
2996 | *
|
2997 | * @name during
|
2998 | * @static
|
2999 | * @memberOf module:ControlFlow
|
3000 | * @method
|
3001 | * @see [async.whilst]{@link module:ControlFlow.whilst}
|
3002 | * @category Control Flow
|
3003 | * @param {Function} test - asynchronous truth test to perform before each
|
3004 | * execution of `fn`. Invoked with (callback).
|
3005 | * @param {Function} fn - A function which is called each time `test` passes.
|
3006 | * The function is passed a `callback(err)`, which must be called once it has
|
3007 | * completed with an optional `err` argument. Invoked with (callback).
|
3008 | * @param {Function} [callback] - A callback which is called after the test
|
3009 | * function has failed and repeated execution of `fn` has stopped. `callback`
|
3010 | * will be passed an error, if one occured, otherwise `null`.
|
3011 | * @example
|
3012 | *
|
3013 | * var count = 0;
|
3014 | *
|
3015 | * async.during(
|
3016 | * function (callback) {
|
3017 | * return callback(null, count < 5);
|
3018 | * },
|
3019 | * function (callback) {
|
3020 | * count++;
|
3021 | * setTimeout(callback, 1000);
|
3022 | * },
|
3023 | * function (err) {
|
3024 | * // 5 seconds have passed
|
3025 | * }
|
3026 | * );
|
3027 | */
|
3028 | function during(test, fn, callback) {
|
3029 | callback = onlyOnce(callback || noop);
|
3030 |
|
3031 | function next(err) {
|
3032 | if (err) return callback(err);
|
3033 | test(check);
|
3034 | }
|
3035 |
|
3036 | function check(err, truth) {
|
3037 | if (err) return callback(err);
|
3038 | if (!truth) return callback(null);
|
3039 | fn(next);
|
3040 | }
|
3041 |
|
3042 | test(check);
|
3043 | }
|
3044 |
|
3045 | function _withoutIndex(iteratee) {
|
3046 | return function (value, index, callback) {
|
3047 | return iteratee(value, callback);
|
3048 | };
|
3049 | }
|
3050 |
|
3051 | /**
|
3052 | * Applies the function `iteratee` to each item in `coll`, in parallel.
|
3053 | * The `iteratee` is called with an item from the list, and a callback for when
|
3054 | * it has finished. If the `iteratee` passes an error to its `callback`, the
|
3055 | * main `callback` (for the `each` function) is immediately called with the
|
3056 | * error.
|
3057 | *
|
3058 | * Note, that since this function applies `iteratee` to each item in parallel,
|
3059 | * there is no guarantee that the iteratee functions will complete in order.
|
3060 | *
|
3061 | * @name each
|
3062 | * @static
|
3063 | * @memberOf module:Collections
|
3064 | * @method
|
3065 | * @alias forEach
|
3066 | * @category Collection
|
3067 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
3068 | * @param {Function} iteratee - A function to apply to each item
|
3069 | * in `coll`. The iteratee is passed a `callback(err)` which must be called once
|
3070 | * it has completed. If no error has occurred, the `callback` should be run
|
3071 | * without arguments or with an explicit `null` argument. The array index is not
|
3072 | * passed to the iteratee. Invoked with (item, callback). If you need the index,
|
3073 | * use `eachOf`.
|
3074 | * @param {Function} [callback] - A callback which is called when all
|
3075 | * `iteratee` functions have finished, or an error occurs. Invoked with (err).
|
3076 | * @example
|
3077 | *
|
3078 | * // assuming openFiles is an array of file names and saveFile is a function
|
3079 | * // to save the modified contents of that file:
|
3080 | *
|
3081 | * async.each(openFiles, saveFile, function(err){
|
3082 | * // if any of the saves produced an error, err would equal that error
|
3083 | * });
|
3084 | *
|
3085 | * // assuming openFiles is an array of file names
|
3086 | * async.each(openFiles, function(file, callback) {
|
3087 | *
|
3088 | * // Perform operation on file here.
|
3089 | * console.log('Processing file ' + file);
|
3090 | *
|
3091 | * if( file.length > 32 ) {
|
3092 | * console.log('This file name is too long');
|
3093 | * callback('File name too long');
|
3094 | * } else {
|
3095 | * // Do work to process file here
|
3096 | * console.log('File processed');
|
3097 | * callback();
|
3098 | * }
|
3099 | * }, function(err) {
|
3100 | * // if any of the file processing produced an error, err would equal that error
|
3101 | * if( err ) {
|
3102 | * // One of the iterations produced an error.
|
3103 | * // All processing will now stop.
|
3104 | * console.log('A file failed to process');
|
3105 | * } else {
|
3106 | * console.log('All files have been processed successfully');
|
3107 | * }
|
3108 | * });
|
3109 | */
|
3110 | function eachLimit(coll, iteratee, callback) {
|
3111 | eachOf(coll, _withoutIndex(iteratee), callback);
|
3112 | }
|
3113 |
|
3114 | /**
|
3115 | * The same as [`each`]{@link module:Collections.each} but runs a maximum of `limit` async operations at a time.
|
3116 | *
|
3117 | * @name eachLimit
|
3118 | * @static
|
3119 | * @memberOf module:Collections
|
3120 | * @method
|
3121 | * @see [async.each]{@link module:Collections.each}
|
3122 | * @alias forEachLimit
|
3123 | * @category Collection
|
3124 | * @param {Array|Iterable|Object} coll - A colleciton to iterate over.
|
3125 | * @param {number} limit - The maximum number of async operations at a time.
|
3126 | * @param {Function} iteratee - A function to apply to each item in `coll`. The
|
3127 | * iteratee is passed a `callback(err)` which must be called once it has
|
3128 | * completed. If no error has occurred, the `callback` should be run without
|
3129 | * arguments or with an explicit `null` argument. The array index is not passed
|
3130 | * to the iteratee. Invoked with (item, callback). If you need the index, use
|
3131 | * `eachOfLimit`.
|
3132 | * @param {Function} [callback] - A callback which is called when all
|
3133 | * `iteratee` functions have finished, or an error occurs. Invoked with (err).
|
3134 | */
|
3135 | function eachLimit$1(coll, limit, iteratee, callback) {
|
3136 | _eachOfLimit(limit)(coll, _withoutIndex(iteratee), callback);
|
3137 | }
|
3138 |
|
3139 | /**
|
3140 | * The same as [`each`]{@link module:Collections.each} but runs only a single async operation at a time.
|
3141 | *
|
3142 | * @name eachSeries
|
3143 | * @static
|
3144 | * @memberOf module:Collections
|
3145 | * @method
|
3146 | * @see [async.each]{@link module:Collections.each}
|
3147 | * @alias forEachSeries
|
3148 | * @category Collection
|
3149 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
3150 | * @param {Function} iteratee - A function to apply to each
|
3151 | * item in `coll`. The iteratee is passed a `callback(err)` which must be called
|
3152 | * once it has completed. If no error has occurred, the `callback` should be run
|
3153 | * without arguments or with an explicit `null` argument. The array index is
|
3154 | * not passed to the iteratee. Invoked with (item, callback). If you need the
|
3155 | * index, use `eachOfSeries`.
|
3156 | * @param {Function} [callback] - A callback which is called when all
|
3157 | * `iteratee` functions have finished, or an error occurs. Invoked with (err).
|
3158 | */
|
3159 | var eachSeries = doLimit(eachLimit$1, 1);
|
3160 |
|
3161 | /**
|
3162 | * Wrap an async function and ensure it calls its callback on a later tick of
|
3163 | * the event loop. If the function already calls its callback on a next tick,
|
3164 | * no extra deferral is added. This is useful for preventing stack overflows
|
3165 | * (`RangeError: Maximum call stack size exceeded`) and generally keeping
|
3166 | * [Zalgo](http://blog.izs.me/post/59142742143/designing-apis-for-asynchrony)
|
3167 | * contained.
|
3168 | *
|
3169 | * @name ensureAsync
|
3170 | * @static
|
3171 | * @memberOf module:Utils
|
3172 | * @method
|
3173 | * @category Util
|
3174 | * @param {Function} fn - an async function, one that expects a node-style
|
3175 | * callback as its last argument.
|
3176 | * @returns {Function} Returns a wrapped function with the exact same call
|
3177 | * signature as the function passed in.
|
3178 | * @example
|
3179 | *
|
3180 | * function sometimesAsync(arg, callback) {
|
3181 | * if (cache[arg]) {
|
3182 | * return callback(null, cache[arg]); // this would be synchronous!!
|
3183 | * } else {
|
3184 | * doSomeIO(arg, callback); // this IO would be asynchronous
|
3185 | * }
|
3186 | * }
|
3187 | *
|
3188 | * // this has a risk of stack overflows if many results are cached in a row
|
3189 | * async.mapSeries(args, sometimesAsync, done);
|
3190 | *
|
3191 | * // this will defer sometimesAsync's callback if necessary,
|
3192 | * // preventing stack overflows
|
3193 | * async.mapSeries(args, async.ensureAsync(sometimesAsync), done);
|
3194 | */
|
3195 | function ensureAsync(fn) {
|
3196 | return initialParams(function (args, callback) {
|
3197 | var sync = true;
|
3198 | args.push(function () {
|
3199 | var innerArgs = arguments;
|
3200 | if (sync) {
|
3201 | setImmediate$1(function () {
|
3202 | callback.apply(null, innerArgs);
|
3203 | });
|
3204 | } else {
|
3205 | callback.apply(null, innerArgs);
|
3206 | }
|
3207 | });
|
3208 | fn.apply(this, args);
|
3209 | sync = false;
|
3210 | });
|
3211 | }
|
3212 |
|
3213 | function notId(v) {
|
3214 | return !v;
|
3215 | }
|
3216 |
|
3217 | /**
|
3218 | * Returns `true` if every element in `coll` satisfies an async test. If any
|
3219 | * iteratee call returns `false`, the main `callback` is immediately called.
|
3220 | *
|
3221 | * @name every
|
3222 | * @static
|
3223 | * @memberOf module:Collections
|
3224 | * @method
|
3225 | * @alias all
|
3226 | * @category Collection
|
3227 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
3228 | * @param {Function} iteratee - A truth test to apply to each item in the
|
3229 | * collection in parallel. The iteratee is passed a `callback(err, truthValue)`
|
3230 | * which must be called with a boolean argument once it has completed. Invoked
|
3231 | * with (item, callback).
|
3232 | * @param {Function} [callback] - A callback which is called after all the
|
3233 | * `iteratee` functions have finished. Result will be either `true` or `false`
|
3234 | * depending on the values of the async tests. Invoked with (err, result).
|
3235 | * @example
|
3236 | *
|
3237 | * async.every(['file1','file2','file3'], function(filePath, callback) {
|
3238 | * fs.access(filePath, function(err) {
|
3239 | * callback(null, !err)
|
3240 | * });
|
3241 | * }, function(err, result) {
|
3242 | * // if result is true then every file exists
|
3243 | * });
|
3244 | */
|
3245 | var every = _createTester(eachOf, notId, notId);
|
3246 |
|
3247 | /**
|
3248 | * The same as [`every`]{@link module:Collections.every} but runs a maximum of `limit` async operations at a time.
|
3249 | *
|
3250 | * @name everyLimit
|
3251 | * @static
|
3252 | * @memberOf module:Collections
|
3253 | * @method
|
3254 | * @see [async.every]{@link module:Collections.every}
|
3255 | * @alias allLimit
|
3256 | * @category Collection
|
3257 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
3258 | * @param {number} limit - The maximum number of async operations at a time.
|
3259 | * @param {Function} iteratee - A truth test to apply to each item in the
|
3260 | * collection in parallel. The iteratee is passed a `callback(err, truthValue)`
|
3261 | * which must be called with a boolean argument once it has completed. Invoked
|
3262 | * with (item, callback).
|
3263 | * @param {Function} [callback] - A callback which is called after all the
|
3264 | * `iteratee` functions have finished. Result will be either `true` or `false`
|
3265 | * depending on the values of the async tests. Invoked with (err, result).
|
3266 | */
|
3267 | var everyLimit = _createTester(eachOfLimit, notId, notId);
|
3268 |
|
3269 | /**
|
3270 | * The same as [`every`]{@link module:Collections.every} but runs only a single async operation at a time.
|
3271 | *
|
3272 | * @name everySeries
|
3273 | * @static
|
3274 | * @memberOf module:Collections
|
3275 | * @method
|
3276 | * @see [async.every]{@link module:Collections.every}
|
3277 | * @alias allSeries
|
3278 | * @category Collection
|
3279 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
3280 | * @param {Function} iteratee - A truth test to apply to each item in the
|
3281 | * collection in parallel. The iteratee is passed a `callback(err, truthValue)`
|
3282 | * which must be called with a boolean argument once it has completed. Invoked
|
3283 | * with (item, callback).
|
3284 | * @param {Function} [callback] - A callback which is called after all the
|
3285 | * `iteratee` functions have finished. Result will be either `true` or `false`
|
3286 | * depending on the values of the async tests. Invoked with (err, result).
|
3287 | */
|
3288 | var everySeries = doLimit(everyLimit, 1);
|
3289 |
|
3290 | function _filter(eachfn, arr, iteratee, callback) {
|
3291 | callback = once(callback || noop);
|
3292 | var results = [];
|
3293 | eachfn(arr, function (x, index, callback) {
|
3294 | iteratee(x, function (err, v) {
|
3295 | if (err) {
|
3296 | callback(err);
|
3297 | } else {
|
3298 | if (v) {
|
3299 | results.push({ index: index, value: x });
|
3300 | }
|
3301 | callback();
|
3302 | }
|
3303 | });
|
3304 | }, function (err) {
|
3305 | if (err) {
|
3306 | callback(err);
|
3307 | } else {
|
3308 | callback(null, arrayMap(results.sort(function (a, b) {
|
3309 | return a.index - b.index;
|
3310 | }), baseProperty('value')));
|
3311 | }
|
3312 | });
|
3313 | }
|
3314 |
|
3315 | /**
|
3316 | * Returns a new array of all the values in `coll` which pass an async truth
|
3317 | * test. This operation is performed in parallel, but the results array will be
|
3318 | * in the same order as the original.
|
3319 | *
|
3320 | * @name filter
|
3321 | * @static
|
3322 | * @memberOf module:Collections
|
3323 | * @method
|
3324 | * @alias select
|
3325 | * @category Collection
|
3326 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
3327 | * @param {Function} iteratee - A truth test to apply to each item in `coll`.
|
3328 | * The `iteratee` is passed a `callback(err, truthValue)`, which must be called
|
3329 | * with a boolean argument once it has completed. Invoked with (item, callback).
|
3330 | * @param {Function} [callback] - A callback which is called after all the
|
3331 | * `iteratee` functions have finished. Invoked with (err, results).
|
3332 | * @example
|
3333 | *
|
3334 | * async.filter(['file1','file2','file3'], function(filePath, callback) {
|
3335 | * fs.access(filePath, function(err) {
|
3336 | * callback(null, !err)
|
3337 | * });
|
3338 | * }, function(err, results) {
|
3339 | * // results now equals an array of the existing files
|
3340 | * });
|
3341 | */
|
3342 | var filter = doParallel(_filter);
|
3343 |
|
3344 | /**
|
3345 | * The same as [`filter`]{@link module:Collections.filter} but runs a maximum of `limit` async operations at a
|
3346 | * time.
|
3347 | *
|
3348 | * @name filterLimit
|
3349 | * @static
|
3350 | * @memberOf module:Collections
|
3351 | * @method
|
3352 | * @see [async.filter]{@link module:Collections.filter}
|
3353 | * @alias selectLimit
|
3354 | * @category Collection
|
3355 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
3356 | * @param {number} limit - The maximum number of async operations at a time.
|
3357 | * @param {Function} iteratee - A truth test to apply to each item in `coll`.
|
3358 | * The `iteratee` is passed a `callback(err, truthValue)`, which must be called
|
3359 | * with a boolean argument once it has completed. Invoked with (item, callback).
|
3360 | * @param {Function} [callback] - A callback which is called after all the
|
3361 | * `iteratee` functions have finished. Invoked with (err, results).
|
3362 | */
|
3363 | var filterLimit = doParallelLimit(_filter);
|
3364 |
|
3365 | /**
|
3366 | * The same as [`filter`]{@link module:Collections.filter} but runs only a single async operation at a time.
|
3367 | *
|
3368 | * @name filterSeries
|
3369 | * @static
|
3370 | * @memberOf module:Collections
|
3371 | * @method
|
3372 | * @see [async.filter]{@link module:Collections.filter}
|
3373 | * @alias selectSeries
|
3374 | * @category Collection
|
3375 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
3376 | * @param {Function} iteratee - A truth test to apply to each item in `coll`.
|
3377 | * The `iteratee` is passed a `callback(err, truthValue)`, which must be called
|
3378 | * with a boolean argument once it has completed. Invoked with (item, callback).
|
3379 | * @param {Function} [callback] - A callback which is called after all the
|
3380 | * `iteratee` functions have finished. Invoked with (err, results)
|
3381 | */
|
3382 | var filterSeries = doLimit(filterLimit, 1);
|
3383 |
|
3384 | /**
|
3385 | * Calls the asynchronous function `fn` with a callback parameter that allows it
|
3386 | * to call itself again, in series, indefinitely.
|
3387 |
|
3388 | * If an error is passed to the
|
3389 | * callback then `errback` is called with the error, and execution stops,
|
3390 | * otherwise it will never be called.
|
3391 | *
|
3392 | * @name forever
|
3393 | * @static
|
3394 | * @memberOf module:ControlFlow
|
3395 | * @method
|
3396 | * @category Control Flow
|
3397 | * @param {Function} fn - a function to call repeatedly. Invoked with (next).
|
3398 | * @param {Function} [errback] - when `fn` passes an error to it's callback,
|
3399 | * this function will be called, and execution stops. Invoked with (err).
|
3400 | * @example
|
3401 | *
|
3402 | * async.forever(
|
3403 | * function(next) {
|
3404 | * // next is suitable for passing to things that need a callback(err [, whatever]);
|
3405 | * // it will result in this function being called again.
|
3406 | * },
|
3407 | * function(err) {
|
3408 | * // if next is called with a value in its first parameter, it will appear
|
3409 | * // in here as 'err', and execution will stop.
|
3410 | * }
|
3411 | * );
|
3412 | */
|
3413 | function forever(fn, errback) {
|
3414 | var done = onlyOnce(errback || noop);
|
3415 | var task = ensureAsync(fn);
|
3416 |
|
3417 | function next(err) {
|
3418 | if (err) return done(err);
|
3419 | task(next);
|
3420 | }
|
3421 | next();
|
3422 | }
|
3423 |
|
3424 | /**
|
3425 | * Logs the result of an `async` function to the `console`. Only works in
|
3426 | * Node.js or in browsers that support `console.log` and `console.error` (such
|
3427 | * as FF and Chrome). If multiple arguments are returned from the async
|
3428 | * function, `console.log` is called on each argument in order.
|
3429 | *
|
3430 | * @name log
|
3431 | * @static
|
3432 | * @memberOf module:Utils
|
3433 | * @method
|
3434 | * @category Util
|
3435 | * @param {Function} function - The function you want to eventually apply all
|
3436 | * arguments to.
|
3437 | * @param {...*} arguments... - Any number of arguments to apply to the function.
|
3438 | * @example
|
3439 | *
|
3440 | * // in a module
|
3441 | * var hello = function(name, callback) {
|
3442 | * setTimeout(function() {
|
3443 | * callback(null, 'hello ' + name);
|
3444 | * }, 1000);
|
3445 | * };
|
3446 | *
|
3447 | * // in the node repl
|
3448 | * node> async.log(hello, 'world');
|
3449 | * 'hello world'
|
3450 | */
|
3451 | var log = consoleFunc('log');
|
3452 |
|
3453 | /**
|
3454 | * The same as [`mapValues`]{@link module:Collections.mapValues} but runs a maximum of `limit` async operations at a
|
3455 | * time.
|
3456 | *
|
3457 | * @name mapValuesLimit
|
3458 | * @static
|
3459 | * @memberOf module:Collections
|
3460 | * @method
|
3461 | * @see [async.mapValues]{@link module:Collections.mapValues}
|
3462 | * @category Collection
|
3463 | * @param {Object} obj - A collection to iterate over.
|
3464 | * @param {number} limit - The maximum number of async operations at a time.
|
3465 | * @param {Function} iteratee - A function to apply to each value in `obj`.
|
3466 | * The iteratee is passed a `callback(err, transformed)` which must be called
|
3467 | * once it has completed with an error (which can be `null`) and a
|
3468 | * transformed value. Invoked with (value, key, callback).
|
3469 | * @param {Function} [callback] - A callback which is called when all `iteratee`
|
3470 | * functions have finished, or an error occurs. Result is an object of the
|
3471 | * transformed values from the `obj`. Invoked with (err, result).
|
3472 | */
|
3473 | function mapValuesLimit(obj, limit, iteratee, callback) {
|
3474 | callback = once(callback || noop);
|
3475 | var newObj = {};
|
3476 | eachOfLimit(obj, limit, function (val, key, next) {
|
3477 | iteratee(val, key, function (err, result) {
|
3478 | if (err) return next(err);
|
3479 | newObj[key] = result;
|
3480 | next();
|
3481 | });
|
3482 | }, function (err) {
|
3483 | callback(err, newObj);
|
3484 | });
|
3485 | }
|
3486 |
|
3487 | /**
|
3488 | * A relative of [`map`]{@link module:Collections.map}, designed for use with objects.
|
3489 | *
|
3490 | * Produces a new Object by mapping each value of `obj` through the `iteratee`
|
3491 | * function. The `iteratee` is called each `value` and `key` from `obj` and a
|
3492 | * callback for when it has finished processing. Each of these callbacks takes
|
3493 | * two arguments: an `error`, and the transformed item from `obj`. If `iteratee`
|
3494 | * passes an error to its callback, the main `callback` (for the `mapValues`
|
3495 | * function) is immediately called with the error.
|
3496 | *
|
3497 | * Note, the order of the keys in the result is not guaranteed. The keys will
|
3498 | * be roughly in the order they complete, (but this is very engine-specific)
|
3499 | *
|
3500 | * @name mapValues
|
3501 | * @static
|
3502 | * @memberOf module:Collections
|
3503 | * @method
|
3504 | * @category Collection
|
3505 | * @param {Object} obj - A collection to iterate over.
|
3506 | * @param {Function} iteratee - A function to apply to each value and key in
|
3507 | * `coll`. The iteratee is passed a `callback(err, transformed)` which must be
|
3508 | * called once it has completed with an error (which can be `null`) and a
|
3509 | * transformed value. Invoked with (value, key, callback).
|
3510 | * @param {Function} [callback] - A callback which is called when all `iteratee`
|
3511 | * functions have finished, or an error occurs. Results is an array of the
|
3512 | * transformed items from the `obj`. Invoked with (err, result).
|
3513 | * @example
|
3514 | *
|
3515 | * async.mapValues({
|
3516 | * f1: 'file1',
|
3517 | * f2: 'file2',
|
3518 | * f3: 'file3'
|
3519 | * }, function (file, key, callback) {
|
3520 | * fs.stat(file, callback);
|
3521 | * }, function(err, result) {
|
3522 | * // results is now a map of stats for each file, e.g.
|
3523 | * // {
|
3524 | * // f1: [stats for file1],
|
3525 | * // f2: [stats for file2],
|
3526 | * // f3: [stats for file3]
|
3527 | * // }
|
3528 | * });
|
3529 | */
|
3530 |
|
3531 | var mapValues = doLimit(mapValuesLimit, Infinity);
|
3532 |
|
3533 | /**
|
3534 | * The same as [`mapValues`]{@link module:Collections.mapValues} but runs only a single async operation at a time.
|
3535 | *
|
3536 | * @name mapValuesSeries
|
3537 | * @static
|
3538 | * @memberOf module:Collections
|
3539 | * @method
|
3540 | * @see [async.mapValues]{@link module:Collections.mapValues}
|
3541 | * @category Collection
|
3542 | * @param {Object} obj - A collection to iterate over.
|
3543 | * @param {Function} iteratee - A function to apply to each value in `obj`.
|
3544 | * The iteratee is passed a `callback(err, transformed)` which must be called
|
3545 | * once it has completed with an error (which can be `null`) and a
|
3546 | * transformed value. Invoked with (value, key, callback).
|
3547 | * @param {Function} [callback] - A callback which is called when all `iteratee`
|
3548 | * functions have finished, or an error occurs. Result is an object of the
|
3549 | * transformed values from the `obj`. Invoked with (err, result).
|
3550 | */
|
3551 | var mapValuesSeries = doLimit(mapValuesLimit, 1);
|
3552 |
|
3553 | function has(obj, key) {
|
3554 | return key in obj;
|
3555 | }
|
3556 |
|
3557 | /**
|
3558 | * Caches the results of an `async` function. When creating a hash to store
|
3559 | * function results against, the callback is omitted from the hash and an
|
3560 | * optional hash function can be used.
|
3561 | *
|
3562 | * If no hash function is specified, the first argument is used as a hash key,
|
3563 | * which may work reasonably if it is a string or a data type that converts to a
|
3564 | * distinct string. Note that objects and arrays will not behave reasonably.
|
3565 | * Neither will cases where the other arguments are significant. In such cases,
|
3566 | * specify your own hash function.
|
3567 | *
|
3568 | * The cache of results is exposed as the `memo` property of the function
|
3569 | * returned by `memoize`.
|
3570 | *
|
3571 | * @name memoize
|
3572 | * @static
|
3573 | * @memberOf module:Utils
|
3574 | * @method
|
3575 | * @category Util
|
3576 | * @param {Function} fn - The function to proxy and cache results from.
|
3577 | * @param {Function} hasher - An optional function for generating a custom hash
|
3578 | * for storing results. It has all the arguments applied to it apart from the
|
3579 | * callback, and must be synchronous.
|
3580 | * @returns {Function} a memoized version of `fn`
|
3581 | * @example
|
3582 | *
|
3583 | * var slow_fn = function(name, callback) {
|
3584 | * // do something
|
3585 | * callback(null, result);
|
3586 | * };
|
3587 | * var fn = async.memoize(slow_fn);
|
3588 | *
|
3589 | * // fn can now be used as if it were slow_fn
|
3590 | * fn('some name', function() {
|
3591 | * // callback
|
3592 | * });
|
3593 | */
|
3594 | function memoize(fn, hasher) {
|
3595 | var memo = Object.create(null);
|
3596 | var queues = Object.create(null);
|
3597 | hasher = hasher || identity;
|
3598 | var memoized = initialParams(function memoized(args, callback) {
|
3599 | var key = hasher.apply(null, args);
|
3600 | if (has(memo, key)) {
|
3601 | setImmediate$1(function () {
|
3602 | callback.apply(null, memo[key]);
|
3603 | });
|
3604 | } else if (has(queues, key)) {
|
3605 | queues[key].push(callback);
|
3606 | } else {
|
3607 | queues[key] = [callback];
|
3608 | fn.apply(null, args.concat([rest(function (args) {
|
3609 | memo[key] = args;
|
3610 | var q = queues[key];
|
3611 | delete queues[key];
|
3612 | for (var i = 0, l = q.length; i < l; i++) {
|
3613 | q[i].apply(null, args);
|
3614 | }
|
3615 | })]));
|
3616 | }
|
3617 | });
|
3618 | memoized.memo = memo;
|
3619 | memoized.unmemoized = fn;
|
3620 | return memoized;
|
3621 | }
|
3622 |
|
3623 | /**
|
3624 | * Calls `callback` on a later loop around the event loop. In Node.js this just
|
3625 | * calls `setImmediate`. In the browser it will use `setImmediate` if
|
3626 | * available, otherwise `setTimeout(callback, 0)`, which means other higher
|
3627 | * priority events may precede the execution of `callback`.
|
3628 | *
|
3629 | * This is used internally for browser-compatibility purposes.
|
3630 | *
|
3631 | * @name nextTick
|
3632 | * @static
|
3633 | * @memberOf module:Utils
|
3634 | * @method
|
3635 | * @alias setImmediate
|
3636 | * @category Util
|
3637 | * @param {Function} callback - The function to call on a later loop around
|
3638 | * the event loop. Invoked with (args...).
|
3639 | * @param {...*} args... - any number of additional arguments to pass to the
|
3640 | * callback on the next tick.
|
3641 | * @example
|
3642 | *
|
3643 | * var call_order = [];
|
3644 | * async.nextTick(function() {
|
3645 | * call_order.push('two');
|
3646 | * // call_order now equals ['one','two']
|
3647 | * });
|
3648 | * call_order.push('one');
|
3649 | *
|
3650 | * async.setImmediate(function (a, b, c) {
|
3651 | * // a, b, and c equal 1, 2, and 3
|
3652 | * }, 1, 2, 3);
|
3653 | */
|
3654 | var _defer$1;
|
3655 |
|
3656 | if (hasNextTick) {
|
3657 | _defer$1 = process.nextTick;
|
3658 | } else if (hasSetImmediate) {
|
3659 | _defer$1 = setImmediate;
|
3660 | } else {
|
3661 | _defer$1 = fallback;
|
3662 | }
|
3663 |
|
3664 | var nextTick = wrap(_defer$1);
|
3665 |
|
3666 | function _parallel(eachfn, tasks, callback) {
|
3667 | callback = callback || noop;
|
3668 | var results = isArrayLike(tasks) ? [] : {};
|
3669 |
|
3670 | eachfn(tasks, function (task, key, callback) {
|
3671 | task(rest(function (err, args) {
|
3672 | if (args.length <= 1) {
|
3673 | args = args[0];
|
3674 | }
|
3675 | results[key] = args;
|
3676 | callback(err);
|
3677 | }));
|
3678 | }, function (err) {
|
3679 | callback(err, results);
|
3680 | });
|
3681 | }
|
3682 |
|
3683 | /**
|
3684 | * Run the `tasks` collection of functions in parallel, without waiting until
|
3685 | * the previous function has completed. If any of the functions pass an error to
|
3686 | * its callback, the main `callback` is immediately called with the value of the
|
3687 | * error. Once the `tasks` have completed, the results are passed to the final
|
3688 | * `callback` as an array.
|
3689 | *
|
3690 | * **Note:** `parallel` is about kicking-off I/O tasks in parallel, not about
|
3691 | * parallel execution of code. If your tasks do not use any timers or perform
|
3692 | * any I/O, they will actually be executed in series. Any synchronous setup
|
3693 | * sections for each task will happen one after the other. JavaScript remains
|
3694 | * single-threaded.
|
3695 | *
|
3696 | * It is also possible to use an object instead of an array. Each property will
|
3697 | * be run as a function and the results will be passed to the final `callback`
|
3698 | * as an object instead of an array. This can be a more readable way of handling
|
3699 | * results from {@link async.parallel}.
|
3700 | *
|
3701 | * @name parallel
|
3702 | * @static
|
3703 | * @memberOf module:ControlFlow
|
3704 | * @method
|
3705 | * @category Control Flow
|
3706 | * @param {Array|Iterable|Object} tasks - A collection containing functions to run.
|
3707 | * Each function is passed a `callback(err, result)` which it must call on
|
3708 | * completion with an error `err` (which can be `null`) and an optional `result`
|
3709 | * value.
|
3710 | * @param {Function} [callback] - An optional callback to run once all the
|
3711 | * functions have completed successfully. This function gets a results array
|
3712 | * (or object) containing all the result arguments passed to the task callbacks.
|
3713 | * Invoked with (err, results).
|
3714 | * @example
|
3715 | * async.parallel([
|
3716 | * function(callback) {
|
3717 | * setTimeout(function() {
|
3718 | * callback(null, 'one');
|
3719 | * }, 200);
|
3720 | * },
|
3721 | * function(callback) {
|
3722 | * setTimeout(function() {
|
3723 | * callback(null, 'two');
|
3724 | * }, 100);
|
3725 | * }
|
3726 | * ],
|
3727 | * // optional callback
|
3728 | * function(err, results) {
|
3729 | * // the results array will equal ['one','two'] even though
|
3730 | * // the second function had a shorter timeout.
|
3731 | * });
|
3732 | *
|
3733 | * // an example using an object instead of an array
|
3734 | * async.parallel({
|
3735 | * one: function(callback) {
|
3736 | * setTimeout(function() {
|
3737 | * callback(null, 1);
|
3738 | * }, 200);
|
3739 | * },
|
3740 | * two: function(callback) {
|
3741 | * setTimeout(function() {
|
3742 | * callback(null, 2);
|
3743 | * }, 100);
|
3744 | * }
|
3745 | * }, function(err, results) {
|
3746 | * // results is now equals to: {one: 1, two: 2}
|
3747 | * });
|
3748 | */
|
3749 | function parallelLimit(tasks, callback) {
|
3750 | _parallel(eachOf, tasks, callback);
|
3751 | }
|
3752 |
|
3753 | /**
|
3754 | * The same as [`parallel`]{@link module:ControlFlow.parallel} but runs a maximum of `limit` async operations at a
|
3755 | * time.
|
3756 | *
|
3757 | * @name parallelLimit
|
3758 | * @static
|
3759 | * @memberOf module:ControlFlow
|
3760 | * @method
|
3761 | * @see [async.parallel]{@link module:ControlFlow.parallel}
|
3762 | * @category Control Flow
|
3763 | * @param {Array|Collection} tasks - A collection containing functions to run.
|
3764 | * Each function is passed a `callback(err, result)` which it must call on
|
3765 | * completion with an error `err` (which can be `null`) and an optional `result`
|
3766 | * value.
|
3767 | * @param {number} limit - The maximum number of async operations at a time.
|
3768 | * @param {Function} [callback] - An optional callback to run once all the
|
3769 | * functions have completed successfully. This function gets a results array
|
3770 | * (or object) containing all the result arguments passed to the task callbacks.
|
3771 | * Invoked with (err, results).
|
3772 | */
|
3773 | function parallelLimit$1(tasks, limit, callback) {
|
3774 | _parallel(_eachOfLimit(limit), tasks, callback);
|
3775 | }
|
3776 |
|
3777 | /**
|
3778 | * A queue of tasks for the worker function to complete.
|
3779 | * @typedef {Object} QueueObject
|
3780 | * @memberOf module:ControlFlow
|
3781 | * @property {Function} length - a function returning the number of items
|
3782 | * waiting to be processed. Invoke with `queue.length()`.
|
3783 | * @property {boolean} started - a boolean indicating whether or not any
|
3784 | * items have been pushed and processed by the queue.
|
3785 | * @property {Function} running - a function returning the number of items
|
3786 | * currently being processed. Invoke with `queue.running()`.
|
3787 | * @property {Function} workersList - a function returning the array of items
|
3788 | * currently being processed. Invoke with `queue.workersList()`.
|
3789 | * @property {Function} idle - a function returning false if there are items
|
3790 | * waiting or being processed, or true if not. Invoke with `queue.idle()`.
|
3791 | * @property {number} concurrency - an integer for determining how many `worker`
|
3792 | * functions should be run in parallel. This property can be changed after a
|
3793 | * `queue` is created to alter the concurrency on-the-fly.
|
3794 | * @property {Function} push - add a new task to the `queue`. Calls `callback`
|
3795 | * once the `worker` has finished processing the task. Instead of a single task,
|
3796 | * a `tasks` array can be submitted. The respective callback is used for every
|
3797 | * task in the list. Invoke with `queue.push(task, [callback])`,
|
3798 | * @property {Function} unshift - add a new task to the front of the `queue`.
|
3799 | * Invoke with `queue.unshift(task, [callback])`.
|
3800 | * @property {Function} saturated - a callback that is called when the number of
|
3801 | * running workers hits the `concurrency` limit, and further tasks will be
|
3802 | * queued.
|
3803 | * @property {Function} unsaturated - a callback that is called when the number
|
3804 | * of running workers is less than the `concurrency` & `buffer` limits, and
|
3805 | * further tasks will not be queued.
|
3806 | * @property {number} buffer - A minimum threshold buffer in order to say that
|
3807 | * the `queue` is `unsaturated`.
|
3808 | * @property {Function} empty - a callback that is called when the last item
|
3809 | * from the `queue` is given to a `worker`.
|
3810 | * @property {Function} drain - a callback that is called when the last item
|
3811 | * from the `queue` has returned from the `worker`.
|
3812 | * @property {Function} error - a callback that is called when a task errors.
|
3813 | * Has the signature `function(error, task)`.
|
3814 | * @property {boolean} paused - a boolean for determining whether the queue is
|
3815 | * in a paused state.
|
3816 | * @property {Function} pause - a function that pauses the processing of tasks
|
3817 | * until `resume()` is called. Invoke with `queue.pause()`.
|
3818 | * @property {Function} resume - a function that resumes the processing of
|
3819 | * queued tasks when the queue is paused. Invoke with `queue.resume()`.
|
3820 | * @property {Function} kill - a function that removes the `drain` callback and
|
3821 | * empties remaining tasks from the queue forcing it to go idle. Invoke with `queue.kill()`.
|
3822 | */
|
3823 |
|
3824 | /**
|
3825 | * Creates a `queue` object with the specified `concurrency`. Tasks added to the
|
3826 | * `queue` are processed in parallel (up to the `concurrency` limit). If all
|
3827 | * `worker`s are in progress, the task is queued until one becomes available.
|
3828 | * Once a `worker` completes a `task`, that `task`'s callback is called.
|
3829 | *
|
3830 | * @name queue
|
3831 | * @static
|
3832 | * @memberOf module:ControlFlow
|
3833 | * @method
|
3834 | * @category Control Flow
|
3835 | * @param {Function} worker - An asynchronous function for processing a queued
|
3836 | * task, which must call its `callback(err)` argument when finished, with an
|
3837 | * optional `error` as an argument. If you want to handle errors from an
|
3838 | * individual task, pass a callback to `q.push()`. Invoked with
|
3839 | * (task, callback).
|
3840 | * @param {number} [concurrency=1] - An `integer` for determining how many
|
3841 | * `worker` functions should be run in parallel. If omitted, the concurrency
|
3842 | * defaults to `1`. If the concurrency is `0`, an error is thrown.
|
3843 | * @returns {module:ControlFlow.QueueObject} A queue object to manage the tasks. Callbacks can
|
3844 | * attached as certain properties to listen for specific events during the
|
3845 | * lifecycle of the queue.
|
3846 | * @example
|
3847 | *
|
3848 | * // create a queue object with concurrency 2
|
3849 | * var q = async.queue(function(task, callback) {
|
3850 | * console.log('hello ' + task.name);
|
3851 | * callback();
|
3852 | * }, 2);
|
3853 | *
|
3854 | * // assign a callback
|
3855 | * q.drain = function() {
|
3856 | * console.log('all items have been processed');
|
3857 | * };
|
3858 | *
|
3859 | * // add some items to the queue
|
3860 | * q.push({name: 'foo'}, function(err) {
|
3861 | * console.log('finished processing foo');
|
3862 | * });
|
3863 | * q.push({name: 'bar'}, function (err) {
|
3864 | * console.log('finished processing bar');
|
3865 | * });
|
3866 | *
|
3867 | * // add some items to the queue (batch-wise)
|
3868 | * q.push([{name: 'baz'},{name: 'bay'},{name: 'bax'}], function(err) {
|
3869 | * console.log('finished processing item');
|
3870 | * });
|
3871 | *
|
3872 | * // add some items to the front of the queue
|
3873 | * q.unshift({name: 'bar'}, function (err) {
|
3874 | * console.log('finished processing bar');
|
3875 | * });
|
3876 | */
|
3877 | function queue$1 (worker, concurrency) {
|
3878 | return queue(function (items, cb) {
|
3879 | worker(items[0], cb);
|
3880 | }, concurrency, 1);
|
3881 | }
|
3882 |
|
3883 | /**
|
3884 | * The same as [async.queue]{@link module:ControlFlow.queue} only tasks are assigned a priority and
|
3885 | * completed in ascending priority order.
|
3886 | *
|
3887 | * @name priorityQueue
|
3888 | * @static
|
3889 | * @memberOf module:ControlFlow
|
3890 | * @method
|
3891 | * @see [async.queue]{@link module:ControlFlow.queue}
|
3892 | * @category Control Flow
|
3893 | * @param {Function} worker - An asynchronous function for processing a queued
|
3894 | * task, which must call its `callback(err)` argument when finished, with an
|
3895 | * optional `error` as an argument. If you want to handle errors from an
|
3896 | * individual task, pass a callback to `q.push()`. Invoked with
|
3897 | * (task, callback).
|
3898 | * @param {number} concurrency - An `integer` for determining how many `worker`
|
3899 | * functions should be run in parallel. If omitted, the concurrency defaults to
|
3900 | * `1`. If the concurrency is `0`, an error is thrown.
|
3901 | * @returns {module:ControlFlow.QueueObject} A priorityQueue object to manage the tasks. There are two
|
3902 | * differences between `queue` and `priorityQueue` objects:
|
3903 | * * `push(task, priority, [callback])` - `priority` should be a number. If an
|
3904 | * array of `tasks` is given, all tasks will be assigned the same priority.
|
3905 | * * The `unshift` method was removed.
|
3906 | */
|
3907 | function priorityQueue (worker, concurrency) {
|
3908 | // Start with a normal queue
|
3909 | var q = queue$1(worker, concurrency);
|
3910 |
|
3911 | // Override push to accept second parameter representing priority
|
3912 | q.push = function (data, priority, callback) {
|
3913 | if (callback == null) callback = noop;
|
3914 | if (typeof callback !== 'function') {
|
3915 | throw new Error('task callback must be a function');
|
3916 | }
|
3917 | q.started = true;
|
3918 | if (!isArray(data)) {
|
3919 | data = [data];
|
3920 | }
|
3921 | if (data.length === 0) {
|
3922 | // call drain immediately if there are no tasks
|
3923 | return setImmediate$1(function () {
|
3924 | q.drain();
|
3925 | });
|
3926 | }
|
3927 |
|
3928 | priority = priority || 0;
|
3929 | var nextNode = q._tasks.head;
|
3930 | while (nextNode && priority >= nextNode.priority) {
|
3931 | nextNode = nextNode.next;
|
3932 | }
|
3933 |
|
3934 | arrayEach(data, function (task) {
|
3935 | var item = {
|
3936 | data: task,
|
3937 | priority: priority,
|
3938 | callback: callback
|
3939 | };
|
3940 |
|
3941 | if (nextNode) {
|
3942 | q._tasks.insertBefore(nextNode, item);
|
3943 | } else {
|
3944 | q._tasks.push(item);
|
3945 | }
|
3946 | });
|
3947 | setImmediate$1(q.process);
|
3948 | };
|
3949 |
|
3950 | // Remove unshift function
|
3951 | delete q.unshift;
|
3952 |
|
3953 | return q;
|
3954 | }
|
3955 |
|
3956 | /**
|
3957 | * Runs the `tasks` array of functions in parallel, without waiting until the
|
3958 | * previous function has completed. Once any the `tasks` completed or pass an
|
3959 | * error to its callback, the main `callback` is immediately called. It's
|
3960 | * equivalent to `Promise.race()`.
|
3961 | *
|
3962 | * @name race
|
3963 | * @static
|
3964 | * @memberOf module:ControlFlow
|
3965 | * @method
|
3966 | * @category Control Flow
|
3967 | * @param {Array} tasks - An array containing functions to run. Each function
|
3968 | * is passed a `callback(err, result)` which it must call on completion with an
|
3969 | * error `err` (which can be `null`) and an optional `result` value.
|
3970 | * @param {Function} callback - A callback to run once any of the functions have
|
3971 | * completed. This function gets an error or result from the first function that
|
3972 | * completed. Invoked with (err, result).
|
3973 | * @returns undefined
|
3974 | * @example
|
3975 | *
|
3976 | * async.race([
|
3977 | * function(callback) {
|
3978 | * setTimeout(function() {
|
3979 | * callback(null, 'one');
|
3980 | * }, 200);
|
3981 | * },
|
3982 | * function(callback) {
|
3983 | * setTimeout(function() {
|
3984 | * callback(null, 'two');
|
3985 | * }, 100);
|
3986 | * }
|
3987 | * ],
|
3988 | * // main callback
|
3989 | * function(err, result) {
|
3990 | * // the result will be equal to 'two' as it finishes earlier
|
3991 | * });
|
3992 | */
|
3993 | function race(tasks, callback) {
|
3994 | callback = once(callback || noop);
|
3995 | if (!isArray(tasks)) return callback(new TypeError('First argument to race must be an array of functions'));
|
3996 | if (!tasks.length) return callback();
|
3997 | arrayEach(tasks, function (task) {
|
3998 | task(callback);
|
3999 | });
|
4000 | }
|
4001 |
|
4002 | var slice = Array.prototype.slice;
|
4003 |
|
4004 | /**
|
4005 | * Same as [`reduce`]{@link module:Collections.reduce}, only operates on `array` in reverse order.
|
4006 | *
|
4007 | * @name reduceRight
|
4008 | * @static
|
4009 | * @memberOf module:Collections
|
4010 | * @method
|
4011 | * @see [async.reduce]{@link module:Collections.reduce}
|
4012 | * @alias foldr
|
4013 | * @category Collection
|
4014 | * @param {Array} array - A collection to iterate over.
|
4015 | * @param {*} memo - The initial state of the reduction.
|
4016 | * @param {Function} iteratee - A function applied to each item in the
|
4017 | * array to produce the next step in the reduction. The `iteratee` is passed a
|
4018 | * `callback(err, reduction)` which accepts an optional error as its first
|
4019 | * argument, and the state of the reduction as the second. If an error is
|
4020 | * passed to the callback, the reduction is stopped and the main `callback` is
|
4021 | * immediately called with the error. Invoked with (memo, item, callback).
|
4022 | * @param {Function} [callback] - A callback which is called after all the
|
4023 | * `iteratee` functions have finished. Result is the reduced value. Invoked with
|
4024 | * (err, result).
|
4025 | */
|
4026 | function reduceRight(array, memo, iteratee, callback) {
|
4027 | var reversed = slice.call(array).reverse();
|
4028 | reduce(reversed, memo, iteratee, callback);
|
4029 | }
|
4030 |
|
4031 | /**
|
4032 | * Wraps the function in another function that always returns data even when it
|
4033 | * errors.
|
4034 | *
|
4035 | * The object returned has either the property `error` or `value`.
|
4036 | *
|
4037 | * @name reflect
|
4038 | * @static
|
4039 | * @memberOf module:Utils
|
4040 | * @method
|
4041 | * @category Util
|
4042 | * @param {Function} fn - The function you want to wrap
|
4043 | * @returns {Function} - A function that always passes null to it's callback as
|
4044 | * the error. The second argument to the callback will be an `object` with
|
4045 | * either an `error` or a `value` property.
|
4046 | * @example
|
4047 | *
|
4048 | * async.parallel([
|
4049 | * async.reflect(function(callback) {
|
4050 | * // do some stuff ...
|
4051 | * callback(null, 'one');
|
4052 | * }),
|
4053 | * async.reflect(function(callback) {
|
4054 | * // do some more stuff but error ...
|
4055 | * callback('bad stuff happened');
|
4056 | * }),
|
4057 | * async.reflect(function(callback) {
|
4058 | * // do some more stuff ...
|
4059 | * callback(null, 'two');
|
4060 | * })
|
4061 | * ],
|
4062 | * // optional callback
|
4063 | * function(err, results) {
|
4064 | * // values
|
4065 | * // results[0].value = 'one'
|
4066 | * // results[1].error = 'bad stuff happened'
|
4067 | * // results[2].value = 'two'
|
4068 | * });
|
4069 | */
|
4070 | function reflect(fn) {
|
4071 | return initialParams(function reflectOn(args, reflectCallback) {
|
4072 | args.push(rest(function callback(err, cbArgs) {
|
4073 | if (err) {
|
4074 | reflectCallback(null, {
|
4075 | error: err
|
4076 | });
|
4077 | } else {
|
4078 | var value = null;
|
4079 | if (cbArgs.length === 1) {
|
4080 | value = cbArgs[0];
|
4081 | } else if (cbArgs.length > 1) {
|
4082 | value = cbArgs;
|
4083 | }
|
4084 | reflectCallback(null, {
|
4085 | value: value
|
4086 | });
|
4087 | }
|
4088 | }));
|
4089 |
|
4090 | return fn.apply(this, args);
|
4091 | });
|
4092 | }
|
4093 |
|
4094 | function reject$1(eachfn, arr, iteratee, callback) {
|
4095 | _filter(eachfn, arr, function (value, cb) {
|
4096 | iteratee(value, function (err, v) {
|
4097 | if (err) {
|
4098 | cb(err);
|
4099 | } else {
|
4100 | cb(null, !v);
|
4101 | }
|
4102 | });
|
4103 | }, callback);
|
4104 | }
|
4105 |
|
4106 | /**
|
4107 | * The opposite of [`filter`]{@link module:Collections.filter}. Removes values that pass an `async` truth test.
|
4108 | *
|
4109 | * @name reject
|
4110 | * @static
|
4111 | * @memberOf module:Collections
|
4112 | * @method
|
4113 | * @see [async.filter]{@link module:Collections.filter}
|
4114 | * @category Collection
|
4115 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
4116 | * @param {Function} iteratee - A truth test to apply to each item in `coll`.
|
4117 | * The `iteratee` is passed a `callback(err, truthValue)`, which must be called
|
4118 | * with a boolean argument once it has completed. Invoked with (item, callback).
|
4119 | * @param {Function} [callback] - A callback which is called after all the
|
4120 | * `iteratee` functions have finished. Invoked with (err, results).
|
4121 | * @example
|
4122 | *
|
4123 | * async.reject(['file1','file2','file3'], function(filePath, callback) {
|
4124 | * fs.access(filePath, function(err) {
|
4125 | * callback(null, !err)
|
4126 | * });
|
4127 | * }, function(err, results) {
|
4128 | * // results now equals an array of missing files
|
4129 | * createFiles(results);
|
4130 | * });
|
4131 | */
|
4132 | var reject = doParallel(reject$1);
|
4133 |
|
4134 | /**
|
4135 | * A helper function that wraps an array or an object of functions with reflect.
|
4136 | *
|
4137 | * @name reflectAll
|
4138 | * @static
|
4139 | * @memberOf module:Utils
|
4140 | * @method
|
4141 | * @see [async.reflect]{@link module:Utils.reflect}
|
4142 | * @category Util
|
4143 | * @param {Array} tasks - The array of functions to wrap in `async.reflect`.
|
4144 | * @returns {Array} Returns an array of functions, each function wrapped in
|
4145 | * `async.reflect`
|
4146 | * @example
|
4147 | *
|
4148 | * let tasks = [
|
4149 | * function(callback) {
|
4150 | * setTimeout(function() {
|
4151 | * callback(null, 'one');
|
4152 | * }, 200);
|
4153 | * },
|
4154 | * function(callback) {
|
4155 | * // do some more stuff but error ...
|
4156 | * callback(new Error('bad stuff happened'));
|
4157 | * },
|
4158 | * function(callback) {
|
4159 | * setTimeout(function() {
|
4160 | * callback(null, 'two');
|
4161 | * }, 100);
|
4162 | * }
|
4163 | * ];
|
4164 | *
|
4165 | * async.parallel(async.reflectAll(tasks),
|
4166 | * // optional callback
|
4167 | * function(err, results) {
|
4168 | * // values
|
4169 | * // results[0].value = 'one'
|
4170 | * // results[1].error = Error('bad stuff happened')
|
4171 | * // results[2].value = 'two'
|
4172 | * });
|
4173 | *
|
4174 | * // an example using an object instead of an array
|
4175 | * let tasks = {
|
4176 | * one: function(callback) {
|
4177 | * setTimeout(function() {
|
4178 | * callback(null, 'one');
|
4179 | * }, 200);
|
4180 | * },
|
4181 | * two: function(callback) {
|
4182 | * callback('two');
|
4183 | * },
|
4184 | * three: function(callback) {
|
4185 | * setTimeout(function() {
|
4186 | * callback(null, 'three');
|
4187 | * }, 100);
|
4188 | * }
|
4189 | * };
|
4190 | *
|
4191 | * async.parallel(async.reflectAll(tasks),
|
4192 | * // optional callback
|
4193 | * function(err, results) {
|
4194 | * // values
|
4195 | * // results.one.value = 'one'
|
4196 | * // results.two.error = 'two'
|
4197 | * // results.three.value = 'three'
|
4198 | * });
|
4199 | */
|
4200 | function reflectAll(tasks) {
|
4201 | var results;
|
4202 | if (isArray(tasks)) {
|
4203 | results = arrayMap(tasks, reflect);
|
4204 | } else {
|
4205 | results = {};
|
4206 | baseForOwn(tasks, function (task, key) {
|
4207 | results[key] = reflect.call(this, task);
|
4208 | });
|
4209 | }
|
4210 | return results;
|
4211 | }
|
4212 |
|
4213 | /**
|
4214 | * The same as [`reject`]{@link module:Collections.reject} but runs a maximum of `limit` async operations at a
|
4215 | * time.
|
4216 | *
|
4217 | * @name rejectLimit
|
4218 | * @static
|
4219 | * @memberOf module:Collections
|
4220 | * @method
|
4221 | * @see [async.reject]{@link module:Collections.reject}
|
4222 | * @category Collection
|
4223 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
4224 | * @param {number} limit - The maximum number of async operations at a time.
|
4225 | * @param {Function} iteratee - A truth test to apply to each item in `coll`.
|
4226 | * The `iteratee` is passed a `callback(err, truthValue)`, which must be called
|
4227 | * with a boolean argument once it has completed. Invoked with (item, callback).
|
4228 | * @param {Function} [callback] - A callback which is called after all the
|
4229 | * `iteratee` functions have finished. Invoked with (err, results).
|
4230 | */
|
4231 | var rejectLimit = doParallelLimit(reject$1);
|
4232 |
|
4233 | /**
|
4234 | * The same as [`reject`]{@link module:Collections.reject} but runs only a single async operation at a time.
|
4235 | *
|
4236 | * @name rejectSeries
|
4237 | * @static
|
4238 | * @memberOf module:Collections
|
4239 | * @method
|
4240 | * @see [async.reject]{@link module:Collections.reject}
|
4241 | * @category Collection
|
4242 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
4243 | * @param {Function} iteratee - A truth test to apply to each item in `coll`.
|
4244 | * The `iteratee` is passed a `callback(err, truthValue)`, which must be called
|
4245 | * with a boolean argument once it has completed. Invoked with (item, callback).
|
4246 | * @param {Function} [callback] - A callback which is called after all the
|
4247 | * `iteratee` functions have finished. Invoked with (err, results).
|
4248 | */
|
4249 | var rejectSeries = doLimit(rejectLimit, 1);
|
4250 |
|
4251 | /**
|
4252 | * Creates a function that returns `value`.
|
4253 | *
|
4254 | * @static
|
4255 | * @memberOf _
|
4256 | * @since 2.4.0
|
4257 | * @category Util
|
4258 | * @param {*} value The value to return from the new function.
|
4259 | * @returns {Function} Returns the new constant function.
|
4260 | * @example
|
4261 | *
|
4262 | * var objects = _.times(2, _.constant({ 'a': 1 }));
|
4263 | *
|
4264 | * console.log(objects);
|
4265 | * // => [{ 'a': 1 }, { 'a': 1 }]
|
4266 | *
|
4267 | * console.log(objects[0] === objects[1]);
|
4268 | * // => true
|
4269 | */
|
4270 | function constant$1(value) {
|
4271 | return function() {
|
4272 | return value;
|
4273 | };
|
4274 | }
|
4275 |
|
4276 | /**
|
4277 | * Attempts to get a successful response from `task` no more than `times` times
|
4278 | * before returning an error. If the task is successful, the `callback` will be
|
4279 | * passed the result of the successful task. If all attempts fail, the callback
|
4280 | * will be passed the error and result (if any) of the final attempt.
|
4281 | *
|
4282 | * @name retry
|
4283 | * @static
|
4284 | * @memberOf module:ControlFlow
|
4285 | * @method
|
4286 | * @category Control Flow
|
4287 | * @param {Object|number} [opts = {times: 5, interval: 0}| 5] - Can be either an
|
4288 | * object with `times` and `interval` or a number.
|
4289 | * * `times` - The number of attempts to make before giving up. The default
|
4290 | * is `5`.
|
4291 | * * `interval` - The time to wait between retries, in milliseconds. The
|
4292 | * default is `0`. The interval may also be specified as a function of the
|
4293 | * retry count (see example).
|
4294 | * * If `opts` is a number, the number specifies the number of times to retry,
|
4295 | * with the default interval of `0`.
|
4296 | * @param {Function} task - A function which receives two arguments: (1) a
|
4297 | * `callback(err, result)` which must be called when finished, passing `err`
|
4298 | * (which can be `null`) and the `result` of the function's execution, and (2)
|
4299 | * a `results` object, containing the results of the previously executed
|
4300 | * functions (if nested inside another control flow). Invoked with
|
4301 | * (callback, results).
|
4302 | * @param {Function} [callback] - An optional callback which is called when the
|
4303 | * task has succeeded, or after the final failed attempt. It receives the `err`
|
4304 | * and `result` arguments of the last attempt at completing the `task`. Invoked
|
4305 | * with (err, results).
|
4306 | * @example
|
4307 | *
|
4308 | * // The `retry` function can be used as a stand-alone control flow by passing
|
4309 | * // a callback, as shown below:
|
4310 | *
|
4311 | * // try calling apiMethod 3 times
|
4312 | * async.retry(3, apiMethod, function(err, result) {
|
4313 | * // do something with the result
|
4314 | * });
|
4315 | *
|
4316 | * // try calling apiMethod 3 times, waiting 200 ms between each retry
|
4317 | * async.retry({times: 3, interval: 200}, apiMethod, function(err, result) {
|
4318 | * // do something with the result
|
4319 | * });
|
4320 | *
|
4321 | * // try calling apiMethod 10 times with exponential backoff
|
4322 | * // (i.e. intervals of 100, 200, 400, 800, 1600, ... milliseconds)
|
4323 | * async.retry({
|
4324 | * times: 10,
|
4325 | * interval: function(retryCount) {
|
4326 | * return 50 * Math.pow(2, retryCount);
|
4327 | * }
|
4328 | * }, apiMethod, function(err, result) {
|
4329 | * // do something with the result
|
4330 | * });
|
4331 | *
|
4332 | * // try calling apiMethod the default 5 times no delay between each retry
|
4333 | * async.retry(apiMethod, function(err, result) {
|
4334 | * // do something with the result
|
4335 | * });
|
4336 | *
|
4337 | * // It can also be embedded within other control flow functions to retry
|
4338 | * // individual methods that are not as reliable, like this:
|
4339 | * async.auto({
|
4340 | * users: api.getUsers.bind(api),
|
4341 | * payments: async.retry(3, api.getPayments.bind(api))
|
4342 | * }, function(err, results) {
|
4343 | * // do something with the results
|
4344 | * });
|
4345 | */
|
4346 | function retry(opts, task, callback) {
|
4347 | var DEFAULT_TIMES = 5;
|
4348 | var DEFAULT_INTERVAL = 0;
|
4349 |
|
4350 | var options = {
|
4351 | times: DEFAULT_TIMES,
|
4352 | intervalFunc: constant$1(DEFAULT_INTERVAL)
|
4353 | };
|
4354 |
|
4355 | function parseTimes(acc, t) {
|
4356 | if (typeof t === 'object') {
|
4357 | acc.times = +t.times || DEFAULT_TIMES;
|
4358 |
|
4359 | acc.intervalFunc = typeof t.interval === 'function' ? t.interval : constant$1(+t.interval || DEFAULT_INTERVAL);
|
4360 | } else if (typeof t === 'number' || typeof t === 'string') {
|
4361 | acc.times = +t || DEFAULT_TIMES;
|
4362 | } else {
|
4363 | throw new Error("Invalid arguments for async.retry");
|
4364 | }
|
4365 | }
|
4366 |
|
4367 | if (arguments.length < 3 && typeof opts === 'function') {
|
4368 | callback = task || noop;
|
4369 | task = opts;
|
4370 | } else {
|
4371 | parseTimes(options, opts);
|
4372 | callback = callback || noop;
|
4373 | }
|
4374 |
|
4375 | if (typeof task !== 'function') {
|
4376 | throw new Error("Invalid arguments for async.retry");
|
4377 | }
|
4378 |
|
4379 | var attempt = 1;
|
4380 | function retryAttempt() {
|
4381 | task(function (err) {
|
4382 | if (err && attempt++ < options.times) {
|
4383 | setTimeout(retryAttempt, options.intervalFunc(attempt));
|
4384 | } else {
|
4385 | callback.apply(null, arguments);
|
4386 | }
|
4387 | });
|
4388 | }
|
4389 |
|
4390 | retryAttempt();
|
4391 | }
|
4392 |
|
4393 | /**
|
4394 | * A close relative of [`retry`]{@link module:ControlFlow.retry}. This method wraps a task and makes it
|
4395 | * retryable, rather than immediately calling it with retries.
|
4396 | *
|
4397 | * @name retryable
|
4398 | * @static
|
4399 | * @memberOf module:ControlFlow
|
4400 | * @method
|
4401 | * @see [async.retry]{@link module:ControlFlow.retry}
|
4402 | * @category Control Flow
|
4403 | * @param {Object|number} [opts = {times: 5, interval: 0}| 5] - optional
|
4404 | * options, exactly the same as from `retry`
|
4405 | * @param {Function} task - the asynchronous function to wrap
|
4406 | * @returns {Functions} The wrapped function, which when invoked, will retry on
|
4407 | * an error, based on the parameters specified in `opts`.
|
4408 | * @example
|
4409 | *
|
4410 | * async.auto({
|
4411 | * dep1: async.retryable(3, getFromFlakyService),
|
4412 | * process: ["dep1", async.retryable(3, function (results, cb) {
|
4413 | * maybeProcessData(results.dep1, cb);
|
4414 | * })]
|
4415 | * }, callback);
|
4416 | */
|
4417 | function retryable (opts, task) {
|
4418 | if (!task) {
|
4419 | task = opts;
|
4420 | opts = null;
|
4421 | }
|
4422 | return initialParams(function (args, callback) {
|
4423 | function taskFn(cb) {
|
4424 | task.apply(null, args.concat([cb]));
|
4425 | }
|
4426 |
|
4427 | if (opts) retry(opts, taskFn, callback);else retry(taskFn, callback);
|
4428 | });
|
4429 | }
|
4430 |
|
4431 | /**
|
4432 | * Run the functions in the `tasks` collection in series, each one running once
|
4433 | * the previous function has completed. If any functions in the series pass an
|
4434 | * error to its callback, no more functions are run, and `callback` is
|
4435 | * immediately called with the value of the error. Otherwise, `callback`
|
4436 | * receives an array of results when `tasks` have completed.
|
4437 | *
|
4438 | * It is also possible to use an object instead of an array. Each property will
|
4439 | * be run as a function, and the results will be passed to the final `callback`
|
4440 | * as an object instead of an array. This can be a more readable way of handling
|
4441 | * results from {@link async.series}.
|
4442 | *
|
4443 | * **Note** that while many implementations preserve the order of object
|
4444 | * properties, the [ECMAScript Language Specification](http://www.ecma-international.org/ecma-262/5.1/#sec-8.6)
|
4445 | * explicitly states that
|
4446 | *
|
4447 | * > The mechanics and order of enumerating the properties is not specified.
|
4448 | *
|
4449 | * So if you rely on the order in which your series of functions are executed,
|
4450 | * and want this to work on all platforms, consider using an array.
|
4451 | *
|
4452 | * @name series
|
4453 | * @static
|
4454 | * @memberOf module:ControlFlow
|
4455 | * @method
|
4456 | * @category Control Flow
|
4457 | * @param {Array|Iterable|Object} tasks - A collection containing functions to run, each
|
4458 | * function is passed a `callback(err, result)` it must call on completion with
|
4459 | * an error `err` (which can be `null`) and an optional `result` value.
|
4460 | * @param {Function} [callback] - An optional callback to run once all the
|
4461 | * functions have completed. This function gets a results array (or object)
|
4462 | * containing all the result arguments passed to the `task` callbacks. Invoked
|
4463 | * with (err, result).
|
4464 | * @example
|
4465 | * async.series([
|
4466 | * function(callback) {
|
4467 | * // do some stuff ...
|
4468 | * callback(null, 'one');
|
4469 | * },
|
4470 | * function(callback) {
|
4471 | * // do some more stuff ...
|
4472 | * callback(null, 'two');
|
4473 | * }
|
4474 | * ],
|
4475 | * // optional callback
|
4476 | * function(err, results) {
|
4477 | * // results is now equal to ['one', 'two']
|
4478 | * });
|
4479 | *
|
4480 | * async.series({
|
4481 | * one: function(callback) {
|
4482 | * setTimeout(function() {
|
4483 | * callback(null, 1);
|
4484 | * }, 200);
|
4485 | * },
|
4486 | * two: function(callback){
|
4487 | * setTimeout(function() {
|
4488 | * callback(null, 2);
|
4489 | * }, 100);
|
4490 | * }
|
4491 | * }, function(err, results) {
|
4492 | * // results is now equal to: {one: 1, two: 2}
|
4493 | * });
|
4494 | */
|
4495 | function series(tasks, callback) {
|
4496 | _parallel(eachOfSeries, tasks, callback);
|
4497 | }
|
4498 |
|
4499 | /**
|
4500 | * Returns `true` if at least one element in the `coll` satisfies an async test.
|
4501 | * If any iteratee call returns `true`, the main `callback` is immediately
|
4502 | * called.
|
4503 | *
|
4504 | * @name some
|
4505 | * @static
|
4506 | * @memberOf module:Collections
|
4507 | * @method
|
4508 | * @alias any
|
4509 | * @category Collection
|
4510 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
4511 | * @param {Function} iteratee - A truth test to apply to each item in the array
|
4512 | * in parallel. The iteratee is passed a `callback(err, truthValue)` which must
|
4513 | * be called with a boolean argument once it has completed. Invoked with
|
4514 | * (item, callback).
|
4515 | * @param {Function} [callback] - A callback which is called as soon as any
|
4516 | * iteratee returns `true`, or after all the iteratee functions have finished.
|
4517 | * Result will be either `true` or `false` depending on the values of the async
|
4518 | * tests. Invoked with (err, result).
|
4519 | * @example
|
4520 | *
|
4521 | * async.some(['file1','file2','file3'], function(filePath, callback) {
|
4522 | * fs.access(filePath, function(err) {
|
4523 | * callback(null, !err)
|
4524 | * });
|
4525 | * }, function(err, result) {
|
4526 | * // if result is true then at least one of the files exists
|
4527 | * });
|
4528 | */
|
4529 | var some = _createTester(eachOf, Boolean, identity);
|
4530 |
|
4531 | /**
|
4532 | * The same as [`some`]{@link module:Collections.some} but runs a maximum of `limit` async operations at a time.
|
4533 | *
|
4534 | * @name someLimit
|
4535 | * @static
|
4536 | * @memberOf module:Collections
|
4537 | * @method
|
4538 | * @see [async.some]{@link module:Collections.some}
|
4539 | * @alias anyLimit
|
4540 | * @category Collection
|
4541 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
4542 | * @param {number} limit - The maximum number of async operations at a time.
|
4543 | * @param {Function} iteratee - A truth test to apply to each item in the array
|
4544 | * in parallel. The iteratee is passed a `callback(err, truthValue)` which must
|
4545 | * be called with a boolean argument once it has completed. Invoked with
|
4546 | * (item, callback).
|
4547 | * @param {Function} [callback] - A callback which is called as soon as any
|
4548 | * iteratee returns `true`, or after all the iteratee functions have finished.
|
4549 | * Result will be either `true` or `false` depending on the values of the async
|
4550 | * tests. Invoked with (err, result).
|
4551 | */
|
4552 | var someLimit = _createTester(eachOfLimit, Boolean, identity);
|
4553 |
|
4554 | /**
|
4555 | * The same as [`some`]{@link module:Collections.some} but runs only a single async operation at a time.
|
4556 | *
|
4557 | * @name someSeries
|
4558 | * @static
|
4559 | * @memberOf module:Collections
|
4560 | * @method
|
4561 | * @see [async.some]{@link module:Collections.some}
|
4562 | * @alias anySeries
|
4563 | * @category Collection
|
4564 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
4565 | * @param {Function} iteratee - A truth test to apply to each item in the array
|
4566 | * in parallel. The iteratee is passed a `callback(err, truthValue)` which must
|
4567 | * be called with a boolean argument once it has completed. Invoked with
|
4568 | * (item, callback).
|
4569 | * @param {Function} [callback] - A callback which is called as soon as any
|
4570 | * iteratee returns `true`, or after all the iteratee functions have finished.
|
4571 | * Result will be either `true` or `false` depending on the values of the async
|
4572 | * tests. Invoked with (err, result).
|
4573 | */
|
4574 | var someSeries = doLimit(someLimit, 1);
|
4575 |
|
4576 | /**
|
4577 | * Sorts a list by the results of running each `coll` value through an async
|
4578 | * `iteratee`.
|
4579 | *
|
4580 | * @name sortBy
|
4581 | * @static
|
4582 | * @memberOf module:Collections
|
4583 | * @method
|
4584 | * @category Collection
|
4585 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
4586 | * @param {Function} iteratee - A function to apply to each item in `coll`.
|
4587 | * The iteratee is passed a `callback(err, sortValue)` which must be called once
|
4588 | * it has completed with an error (which can be `null`) and a value to use as
|
4589 | * the sort criteria. Invoked with (item, callback).
|
4590 | * @param {Function} callback - A callback which is called after all the
|
4591 | * `iteratee` functions have finished, or an error occurs. Results is the items
|
4592 | * from the original `coll` sorted by the values returned by the `iteratee`
|
4593 | * calls. Invoked with (err, results).
|
4594 | * @example
|
4595 | *
|
4596 | * async.sortBy(['file1','file2','file3'], function(file, callback) {
|
4597 | * fs.stat(file, function(err, stats) {
|
4598 | * callback(err, stats.mtime);
|
4599 | * });
|
4600 | * }, function(err, results) {
|
4601 | * // results is now the original array of files sorted by
|
4602 | * // modified date
|
4603 | * });
|
4604 | *
|
4605 | * // By modifying the callback parameter the
|
4606 | * // sorting order can be influenced:
|
4607 | *
|
4608 | * // ascending order
|
4609 | * async.sortBy([1,9,3,5], function(x, callback) {
|
4610 | * callback(null, x);
|
4611 | * }, function(err,result) {
|
4612 | * // result callback
|
4613 | * });
|
4614 | *
|
4615 | * // descending order
|
4616 | * async.sortBy([1,9,3,5], function(x, callback) {
|
4617 | * callback(null, x*-1); //<- x*-1 instead of x, turns the order around
|
4618 | * }, function(err,result) {
|
4619 | * // result callback
|
4620 | * });
|
4621 | */
|
4622 | function sortBy(coll, iteratee, callback) {
|
4623 | map(coll, function (x, callback) {
|
4624 | iteratee(x, function (err, criteria) {
|
4625 | if (err) return callback(err);
|
4626 | callback(null, { value: x, criteria: criteria });
|
4627 | });
|
4628 | }, function (err, results) {
|
4629 | if (err) return callback(err);
|
4630 | callback(null, arrayMap(results.sort(comparator), baseProperty('value')));
|
4631 | });
|
4632 |
|
4633 | function comparator(left, right) {
|
4634 | var a = left.criteria,
|
4635 | b = right.criteria;
|
4636 | return a < b ? -1 : a > b ? 1 : 0;
|
4637 | }
|
4638 | }
|
4639 |
|
4640 | /**
|
4641 | * Sets a time limit on an asynchronous function. If the function does not call
|
4642 | * its callback within the specified milliseconds, it will be called with a
|
4643 | * timeout error. The code property for the error object will be `'ETIMEDOUT'`.
|
4644 | *
|
4645 | * @name timeout
|
4646 | * @static
|
4647 | * @memberOf module:Utils
|
4648 | * @method
|
4649 | * @category Util
|
4650 | * @param {Function} asyncFn - The asynchronous function you want to set the
|
4651 | * time limit.
|
4652 | * @param {number} milliseconds - The specified time limit.
|
4653 | * @param {*} [info] - Any variable you want attached (`string`, `object`, etc)
|
4654 | * to timeout Error for more information..
|
4655 | * @returns {Function} Returns a wrapped function that can be used with any of
|
4656 | * the control flow functions.
|
4657 | * @example
|
4658 | *
|
4659 | * async.timeout(function(callback) {
|
4660 | * doAsyncTask(callback);
|
4661 | * }, 1000);
|
4662 | */
|
4663 | function timeout(asyncFn, milliseconds, info) {
|
4664 | var originalCallback, timer;
|
4665 | var timedOut = false;
|
4666 |
|
4667 | function injectedCallback() {
|
4668 | if (!timedOut) {
|
4669 | originalCallback.apply(null, arguments);
|
4670 | clearTimeout(timer);
|
4671 | }
|
4672 | }
|
4673 |
|
4674 | function timeoutCallback() {
|
4675 | var name = asyncFn.name || 'anonymous';
|
4676 | var error = new Error('Callback function "' + name + '" timed out.');
|
4677 | error.code = 'ETIMEDOUT';
|
4678 | if (info) {
|
4679 | error.info = info;
|
4680 | }
|
4681 | timedOut = true;
|
4682 | originalCallback(error);
|
4683 | }
|
4684 |
|
4685 | return initialParams(function (args, origCallback) {
|
4686 | originalCallback = origCallback;
|
4687 | // setup timer and call original function
|
4688 | timer = setTimeout(timeoutCallback, milliseconds);
|
4689 | asyncFn.apply(null, args.concat(injectedCallback));
|
4690 | });
|
4691 | }
|
4692 |
|
4693 | /* Built-in method references for those with the same name as other `lodash` methods. */
|
4694 | var nativeCeil = Math.ceil;
|
4695 | var nativeMax$1 = Math.max;
|
4696 | /**
|
4697 | * The base implementation of `_.range` and `_.rangeRight` which doesn't
|
4698 | * coerce arguments to numbers.
|
4699 | *
|
4700 | * @private
|
4701 | * @param {number} start The start of the range.
|
4702 | * @param {number} end The end of the range.
|
4703 | * @param {number} step The value to increment or decrement by.
|
4704 | * @param {boolean} [fromRight] Specify iterating from right to left.
|
4705 | * @returns {Array} Returns the range of numbers.
|
4706 | */
|
4707 | function baseRange(start, end, step, fromRight) {
|
4708 | var index = -1,
|
4709 | length = nativeMax$1(nativeCeil((end - start) / (step || 1)), 0),
|
4710 | result = Array(length);
|
4711 |
|
4712 | while (length--) {
|
4713 | result[fromRight ? length : ++index] = start;
|
4714 | start += step;
|
4715 | }
|
4716 | return result;
|
4717 | }
|
4718 |
|
4719 | /**
|
4720 | * The same as [times]{@link module:ControlFlow.times} but runs a maximum of `limit` async operations at a
|
4721 | * time.
|
4722 | *
|
4723 | * @name timesLimit
|
4724 | * @static
|
4725 | * @memberOf module:ControlFlow
|
4726 | * @method
|
4727 | * @see [async.times]{@link module:ControlFlow.times}
|
4728 | * @category Control Flow
|
4729 | * @param {number} count - The number of times to run the function.
|
4730 | * @param {number} limit - The maximum number of async operations at a time.
|
4731 | * @param {Function} iteratee - The function to call `n` times. Invoked with the
|
4732 | * iteration index and a callback (n, next).
|
4733 | * @param {Function} callback - see [async.map]{@link module:Collections.map}.
|
4734 | */
|
4735 | function timeLimit(count, limit, iteratee, callback) {
|
4736 | mapLimit(baseRange(0, count, 1), limit, iteratee, callback);
|
4737 | }
|
4738 |
|
4739 | /**
|
4740 | * Calls the `iteratee` function `n` times, and accumulates results in the same
|
4741 | * manner you would use with [map]{@link module:Collections.map}.
|
4742 | *
|
4743 | * @name times
|
4744 | * @static
|
4745 | * @memberOf module:ControlFlow
|
4746 | * @method
|
4747 | * @see [async.map]{@link module:Collections.map}
|
4748 | * @category Control Flow
|
4749 | * @param {number} n - The number of times to run the function.
|
4750 | * @param {Function} iteratee - The function to call `n` times. Invoked with the
|
4751 | * iteration index and a callback (n, next).
|
4752 | * @param {Function} callback - see {@link module:Collections.map}.
|
4753 | * @example
|
4754 | *
|
4755 | * // Pretend this is some complicated async factory
|
4756 | * var createUser = function(id, callback) {
|
4757 | * callback(null, {
|
4758 | * id: 'user' + id
|
4759 | * });
|
4760 | * };
|
4761 | *
|
4762 | * // generate 5 users
|
4763 | * async.times(5, function(n, next) {
|
4764 | * createUser(n, function(err, user) {
|
4765 | * next(err, user);
|
4766 | * });
|
4767 | * }, function(err, users) {
|
4768 | * // we should now have 5 users
|
4769 | * });
|
4770 | */
|
4771 | var times = doLimit(timeLimit, Infinity);
|
4772 |
|
4773 | /**
|
4774 | * The same as [times]{@link module:ControlFlow.times} but runs only a single async operation at a time.
|
4775 | *
|
4776 | * @name timesSeries
|
4777 | * @static
|
4778 | * @memberOf module:ControlFlow
|
4779 | * @method
|
4780 | * @see [async.times]{@link module:ControlFlow.times}
|
4781 | * @category Control Flow
|
4782 | * @param {number} n - The number of times to run the function.
|
4783 | * @param {Function} iteratee - The function to call `n` times. Invoked with the
|
4784 | * iteration index and a callback (n, next).
|
4785 | * @param {Function} callback - see {@link module:Collections.map}.
|
4786 | */
|
4787 | var timesSeries = doLimit(timeLimit, 1);
|
4788 |
|
4789 | /**
|
4790 | * A relative of `reduce`. Takes an Object or Array, and iterates over each
|
4791 | * element in series, each step potentially mutating an `accumulator` value.
|
4792 | * The type of the accumulator defaults to the type of collection passed in.
|
4793 | *
|
4794 | * @name transform
|
4795 | * @static
|
4796 | * @memberOf module:Collections
|
4797 | * @method
|
4798 | * @category Collection
|
4799 | * @param {Array|Iterable|Object} coll - A collection to iterate over.
|
4800 | * @param {*} [accumulator] - The initial state of the transform. If omitted,
|
4801 | * it will default to an empty Object or Array, depending on the type of `coll`
|
4802 | * @param {Function} iteratee - A function applied to each item in the
|
4803 | * collection that potentially modifies the accumulator. The `iteratee` is
|
4804 | * passed a `callback(err)` which accepts an optional error as its first
|
4805 | * argument. If an error is passed to the callback, the transform is stopped
|
4806 | * and the main `callback` is immediately called with the error.
|
4807 | * Invoked with (accumulator, item, key, callback).
|
4808 | * @param {Function} [callback] - A callback which is called after all the
|
4809 | * `iteratee` functions have finished. Result is the transformed accumulator.
|
4810 | * Invoked with (err, result).
|
4811 | * @example
|
4812 | *
|
4813 | * async.transform([1,2,3], function(acc, item, index, callback) {
|
4814 | * // pointless async:
|
4815 | * process.nextTick(function() {
|
4816 | * acc.push(item * 2)
|
4817 | * callback(null)
|
4818 | * });
|
4819 | * }, function(err, result) {
|
4820 | * // result is now equal to [2, 4, 6]
|
4821 | * });
|
4822 | *
|
4823 | * @example
|
4824 | *
|
4825 | * async.transform({a: 1, b: 2, c: 3}, function (obj, val, key, callback) {
|
4826 | * setImmediate(function () {
|
4827 | * obj[key] = val * 2;
|
4828 | * callback();
|
4829 | * })
|
4830 | * }, function (err, result) {
|
4831 | * // result is equal to {a: 2, b: 4, c: 6}
|
4832 | * })
|
4833 | */
|
4834 | function transform(coll, accumulator, iteratee, callback) {
|
4835 | if (arguments.length === 3) {
|
4836 | callback = iteratee;
|
4837 | iteratee = accumulator;
|
4838 | accumulator = isArray(coll) ? [] : {};
|
4839 | }
|
4840 | callback = once(callback || noop);
|
4841 |
|
4842 | eachOf(coll, function (v, k, cb) {
|
4843 | iteratee(accumulator, v, k, cb);
|
4844 | }, function (err) {
|
4845 | callback(err, accumulator);
|
4846 | });
|
4847 | }
|
4848 |
|
4849 | /**
|
4850 | * Undoes a [memoize]{@link module:Utils.memoize}d function, reverting it to the original,
|
4851 | * unmemoized form. Handy for testing.
|
4852 | *
|
4853 | * @name unmemoize
|
4854 | * @static
|
4855 | * @memberOf module:Utils
|
4856 | * @method
|
4857 | * @see [async.memoize]{@link module:Utils.memoize}
|
4858 | * @category Util
|
4859 | * @param {Function} fn - the memoized function
|
4860 | * @returns {Function} a function that calls the original unmemoized function
|
4861 | */
|
4862 | function unmemoize(fn) {
|
4863 | return function () {
|
4864 | return (fn.unmemoized || fn).apply(null, arguments);
|
4865 | };
|
4866 | }
|
4867 |
|
4868 | /**
|
4869 | * Repeatedly call `fn`, while `test` returns `true`. Calls `callback` when
|
4870 | * stopped, or an error occurs.
|
4871 | *
|
4872 | * @name whilst
|
4873 | * @static
|
4874 | * @memberOf module:ControlFlow
|
4875 | * @method
|
4876 | * @category Control Flow
|
4877 | * @param {Function} test - synchronous truth test to perform before each
|
4878 | * execution of `fn`. Invoked with ().
|
4879 | * @param {Function} iteratee - A function which is called each time `test` passes.
|
4880 | * The function is passed a `callback(err)`, which must be called once it has
|
4881 | * completed with an optional `err` argument. Invoked with (callback).
|
4882 | * @param {Function} [callback] - A callback which is called after the test
|
4883 | * function has failed and repeated execution of `fn` has stopped. `callback`
|
4884 | * will be passed an error and any arguments passed to the final `fn`'s
|
4885 | * callback. Invoked with (err, [results]);
|
4886 | * @returns undefined
|
4887 | * @example
|
4888 | *
|
4889 | * var count = 0;
|
4890 | * async.whilst(
|
4891 | * function() { return count < 5; },
|
4892 | * function(callback) {
|
4893 | * count++;
|
4894 | * setTimeout(function() {
|
4895 | * callback(null, count);
|
4896 | * }, 1000);
|
4897 | * },
|
4898 | * function (err, n) {
|
4899 | * // 5 seconds have passed, n = 5
|
4900 | * }
|
4901 | * );
|
4902 | */
|
4903 | function whilst(test, iteratee, callback) {
|
4904 | callback = onlyOnce(callback || noop);
|
4905 | if (!test()) return callback(null);
|
4906 | var next = rest(function (err, args) {
|
4907 | if (err) return callback(err);
|
4908 | if (test()) return iteratee(next);
|
4909 | callback.apply(null, [null].concat(args));
|
4910 | });
|
4911 | iteratee(next);
|
4912 | }
|
4913 |
|
4914 | /**
|
4915 | * Repeatedly call `fn` until `test` returns `true`. Calls `callback` when
|
4916 | * stopped, or an error occurs. `callback` will be passed an error and any
|
4917 | * arguments passed to the final `fn`'s callback.
|
4918 | *
|
4919 | * The inverse of [whilst]{@link module:ControlFlow.whilst}.
|
4920 | *
|
4921 | * @name until
|
4922 | * @static
|
4923 | * @memberOf module:ControlFlow
|
4924 | * @method
|
4925 | * @see [async.whilst]{@link module:ControlFlow.whilst}
|
4926 | * @category Control Flow
|
4927 | * @param {Function} test - synchronous truth test to perform before each
|
4928 | * execution of `fn`. Invoked with ().
|
4929 | * @param {Function} fn - A function which is called each time `test` fails.
|
4930 | * The function is passed a `callback(err)`, which must be called once it has
|
4931 | * completed with an optional `err` argument. Invoked with (callback).
|
4932 | * @param {Function} [callback] - A callback which is called after the test
|
4933 | * function has passed and repeated execution of `fn` has stopped. `callback`
|
4934 | * will be passed an error and any arguments passed to the final `fn`'s
|
4935 | * callback. Invoked with (err, [results]);
|
4936 | */
|
4937 | function until(test, fn, callback) {
|
4938 | whilst(function () {
|
4939 | return !test.apply(this, arguments);
|
4940 | }, fn, callback);
|
4941 | }
|
4942 |
|
4943 | /**
|
4944 | * Runs the `tasks` array of functions in series, each passing their results to
|
4945 | * the next in the array. However, if any of the `tasks` pass an error to their
|
4946 | * own callback, the next function is not executed, and the main `callback` is
|
4947 | * immediately called with the error.
|
4948 | *
|
4949 | * @name waterfall
|
4950 | * @static
|
4951 | * @memberOf module:ControlFlow
|
4952 | * @method
|
4953 | * @category Control Flow
|
4954 | * @param {Array} tasks - An array of functions to run, each function is passed
|
4955 | * a `callback(err, result1, result2, ...)` it must call on completion. The
|
4956 | * first argument is an error (which can be `null`) and any further arguments
|
4957 | * will be passed as arguments in order to the next task.
|
4958 | * @param {Function} [callback] - An optional callback to run once all the
|
4959 | * functions have completed. This will be passed the results of the last task's
|
4960 | * callback. Invoked with (err, [results]).
|
4961 | * @returns undefined
|
4962 | * @example
|
4963 | *
|
4964 | * async.waterfall([
|
4965 | * function(callback) {
|
4966 | * callback(null, 'one', 'two');
|
4967 | * },
|
4968 | * function(arg1, arg2, callback) {
|
4969 | * // arg1 now equals 'one' and arg2 now equals 'two'
|
4970 | * callback(null, 'three');
|
4971 | * },
|
4972 | * function(arg1, callback) {
|
4973 | * // arg1 now equals 'three'
|
4974 | * callback(null, 'done');
|
4975 | * }
|
4976 | * ], function (err, result) {
|
4977 | * // result now equals 'done'
|
4978 | * });
|
4979 | *
|
4980 | * // Or, with named functions:
|
4981 | * async.waterfall([
|
4982 | * myFirstFunction,
|
4983 | * mySecondFunction,
|
4984 | * myLastFunction,
|
4985 | * ], function (err, result) {
|
4986 | * // result now equals 'done'
|
4987 | * });
|
4988 | * function myFirstFunction(callback) {
|
4989 | * callback(null, 'one', 'two');
|
4990 | * }
|
4991 | * function mySecondFunction(arg1, arg2, callback) {
|
4992 | * // arg1 now equals 'one' and arg2 now equals 'two'
|
4993 | * callback(null, 'three');
|
4994 | * }
|
4995 | * function myLastFunction(arg1, callback) {
|
4996 | * // arg1 now equals 'three'
|
4997 | * callback(null, 'done');
|
4998 | * }
|
4999 | */
|
5000 | function waterfall (tasks, callback) {
|
5001 | callback = once(callback || noop);
|
5002 | if (!isArray(tasks)) return callback(new Error('First argument to waterfall must be an array of functions'));
|
5003 | if (!tasks.length) return callback();
|
5004 | var taskIndex = 0;
|
5005 |
|
5006 | function nextTask(args) {
|
5007 | if (taskIndex === tasks.length) {
|
5008 | return callback.apply(null, [null].concat(args));
|
5009 | }
|
5010 |
|
5011 | var taskCallback = onlyOnce(rest(function (err, args) {
|
5012 | if (err) {
|
5013 | return callback.apply(null, [err].concat(args));
|
5014 | }
|
5015 | nextTask(args);
|
5016 | }));
|
5017 |
|
5018 | args.push(taskCallback);
|
5019 |
|
5020 | var task = tasks[taskIndex++];
|
5021 | task.apply(null, args);
|
5022 | }
|
5023 |
|
5024 | nextTask([]);
|
5025 | }
|
5026 |
|
5027 | var index = {
|
5028 | applyEach: applyEach,
|
5029 | applyEachSeries: applyEachSeries,
|
5030 | apply: apply$1,
|
5031 | asyncify: asyncify,
|
5032 | auto: auto,
|
5033 | autoInject: autoInject,
|
5034 | cargo: cargo,
|
5035 | compose: compose,
|
5036 | concat: concat,
|
5037 | concatSeries: concatSeries,
|
5038 | constant: constant,
|
5039 | detect: detect,
|
5040 | detectLimit: detectLimit,
|
5041 | detectSeries: detectSeries,
|
5042 | dir: dir,
|
5043 | doDuring: doDuring,
|
5044 | doUntil: doUntil,
|
5045 | doWhilst: doWhilst,
|
5046 | during: during,
|
5047 | each: eachLimit,
|
5048 | eachLimit: eachLimit$1,
|
5049 | eachOf: eachOf,
|
5050 | eachOfLimit: eachOfLimit,
|
5051 | eachOfSeries: eachOfSeries,
|
5052 | eachSeries: eachSeries,
|
5053 | ensureAsync: ensureAsync,
|
5054 | every: every,
|
5055 | everyLimit: everyLimit,
|
5056 | everySeries: everySeries,
|
5057 | filter: filter,
|
5058 | filterLimit: filterLimit,
|
5059 | filterSeries: filterSeries,
|
5060 | forever: forever,
|
5061 | log: log,
|
5062 | map: map,
|
5063 | mapLimit: mapLimit,
|
5064 | mapSeries: mapSeries,
|
5065 | mapValues: mapValues,
|
5066 | mapValuesLimit: mapValuesLimit,
|
5067 | mapValuesSeries: mapValuesSeries,
|
5068 | memoize: memoize,
|
5069 | nextTick: nextTick,
|
5070 | parallel: parallelLimit,
|
5071 | parallelLimit: parallelLimit$1,
|
5072 | priorityQueue: priorityQueue,
|
5073 | queue: queue$1,
|
5074 | race: race,
|
5075 | reduce: reduce,
|
5076 | reduceRight: reduceRight,
|
5077 | reflect: reflect,
|
5078 | reflectAll: reflectAll,
|
5079 | reject: reject,
|
5080 | rejectLimit: rejectLimit,
|
5081 | rejectSeries: rejectSeries,
|
5082 | retry: retry,
|
5083 | retryable: retryable,
|
5084 | seq: seq,
|
5085 | series: series,
|
5086 | setImmediate: setImmediate$1,
|
5087 | some: some,
|
5088 | someLimit: someLimit,
|
5089 | someSeries: someSeries,
|
5090 | sortBy: sortBy,
|
5091 | timeout: timeout,
|
5092 | times: times,
|
5093 | timesLimit: timeLimit,
|
5094 | timesSeries: timesSeries,
|
5095 | transform: transform,
|
5096 | unmemoize: unmemoize,
|
5097 | until: until,
|
5098 | waterfall: waterfall,
|
5099 | whilst: whilst,
|
5100 |
|
5101 | // aliases
|
5102 | all: every,
|
5103 | any: some,
|
5104 | forEach: eachLimit,
|
5105 | forEachSeries: eachSeries,
|
5106 | forEachLimit: eachLimit$1,
|
5107 | forEachOf: eachOf,
|
5108 | forEachOfSeries: eachOfSeries,
|
5109 | forEachOfLimit: eachOfLimit,
|
5110 | inject: reduce,
|
5111 | foldl: reduce,
|
5112 | foldr: reduceRight,
|
5113 | select: filter,
|
5114 | selectLimit: filterLimit,
|
5115 | selectSeries: filterSeries,
|
5116 | wrapSync: asyncify
|
5117 | };
|
5118 |
|
5119 | exports['default'] = index;
|
5120 | exports.applyEach = applyEach;
|
5121 | exports.applyEachSeries = applyEachSeries;
|
5122 | exports.apply = apply$1;
|
5123 | exports.asyncify = asyncify;
|
5124 | exports.auto = auto;
|
5125 | exports.autoInject = autoInject;
|
5126 | exports.cargo = cargo;
|
5127 | exports.compose = compose;
|
5128 | exports.concat = concat;
|
5129 | exports.concatSeries = concatSeries;
|
5130 | exports.constant = constant;
|
5131 | exports.detect = detect;
|
5132 | exports.detectLimit = detectLimit;
|
5133 | exports.detectSeries = detectSeries;
|
5134 | exports.dir = dir;
|
5135 | exports.doDuring = doDuring;
|
5136 | exports.doUntil = doUntil;
|
5137 | exports.doWhilst = doWhilst;
|
5138 | exports.during = during;
|
5139 | exports.each = eachLimit;
|
5140 | exports.eachLimit = eachLimit$1;
|
5141 | exports.eachOf = eachOf;
|
5142 | exports.eachOfLimit = eachOfLimit;
|
5143 | exports.eachOfSeries = eachOfSeries;
|
5144 | exports.eachSeries = eachSeries;
|
5145 | exports.ensureAsync = ensureAsync;
|
5146 | exports.every = every;
|
5147 | exports.everyLimit = everyLimit;
|
5148 | exports.everySeries = everySeries;
|
5149 | exports.filter = filter;
|
5150 | exports.filterLimit = filterLimit;
|
5151 | exports.filterSeries = filterSeries;
|
5152 | exports.forever = forever;
|
5153 | exports.log = log;
|
5154 | exports.map = map;
|
5155 | exports.mapLimit = mapLimit;
|
5156 | exports.mapSeries = mapSeries;
|
5157 | exports.mapValues = mapValues;
|
5158 | exports.mapValuesLimit = mapValuesLimit;
|
5159 | exports.mapValuesSeries = mapValuesSeries;
|
5160 | exports.memoize = memoize;
|
5161 | exports.nextTick = nextTick;
|
5162 | exports.parallel = parallelLimit;
|
5163 | exports.parallelLimit = parallelLimit$1;
|
5164 | exports.priorityQueue = priorityQueue;
|
5165 | exports.queue = queue$1;
|
5166 | exports.race = race;
|
5167 | exports.reduce = reduce;
|
5168 | exports.reduceRight = reduceRight;
|
5169 | exports.reflect = reflect;
|
5170 | exports.reflectAll = reflectAll;
|
5171 | exports.reject = reject;
|
5172 | exports.rejectLimit = rejectLimit;
|
5173 | exports.rejectSeries = rejectSeries;
|
5174 | exports.retry = retry;
|
5175 | exports.retryable = retryable;
|
5176 | exports.seq = seq;
|
5177 | exports.series = series;
|
5178 | exports.setImmediate = setImmediate$1;
|
5179 | exports.some = some;
|
5180 | exports.someLimit = someLimit;
|
5181 | exports.someSeries = someSeries;
|
5182 | exports.sortBy = sortBy;
|
5183 | exports.timeout = timeout;
|
5184 | exports.times = times;
|
5185 | exports.timesLimit = timeLimit;
|
5186 | exports.timesSeries = timesSeries;
|
5187 | exports.transform = transform;
|
5188 | exports.unmemoize = unmemoize;
|
5189 | exports.until = until;
|
5190 | exports.waterfall = waterfall;
|
5191 | exports.whilst = whilst;
|
5192 | exports.all = every;
|
5193 | exports.allLimit = everyLimit;
|
5194 | exports.allSeries = everySeries;
|
5195 | exports.any = some;
|
5196 | exports.anyLimit = someLimit;
|
5197 | exports.anySeries = someSeries;
|
5198 | exports.find = detect;
|
5199 | exports.findLimit = detectLimit;
|
5200 | exports.findSeries = detectSeries;
|
5201 | exports.forEach = eachLimit;
|
5202 | exports.forEachSeries = eachSeries;
|
5203 | exports.forEachLimit = eachLimit$1;
|
5204 | exports.forEachOf = eachOf;
|
5205 | exports.forEachOfSeries = eachOfSeries;
|
5206 | exports.forEachOfLimit = eachOfLimit;
|
5207 | exports.inject = reduce;
|
5208 | exports.foldl = reduce;
|
5209 | exports.foldr = reduceRight;
|
5210 | exports.select = filter;
|
5211 | exports.selectLimit = filterLimit;
|
5212 | exports.selectSeries = filterSeries;
|
5213 | exports.wrapSync = asyncify;
|
5214 |
|
5215 | })); |
\ | No newline at end of file |