[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = tryEach;
|
---|
| 7 |
|
---|
| 8 | var _noop = require('lodash/noop');
|
---|
| 9 |
|
---|
| 10 | var _noop2 = _interopRequireDefault(_noop);
|
---|
| 11 |
|
---|
| 12 | var _eachSeries = require('./eachSeries');
|
---|
| 13 |
|
---|
| 14 | var _eachSeries2 = _interopRequireDefault(_eachSeries);
|
---|
| 15 |
|
---|
| 16 | var _wrapAsync = require('./internal/wrapAsync');
|
---|
| 17 |
|
---|
| 18 | var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
|
---|
| 19 |
|
---|
| 20 | var _slice = require('./internal/slice');
|
---|
| 21 |
|
---|
| 22 | var _slice2 = _interopRequireDefault(_slice);
|
---|
| 23 |
|
---|
| 24 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
| 25 |
|
---|
| 26 | /**
|
---|
| 27 | * It runs each task in series but stops whenever any of the functions were
|
---|
| 28 | * successful. If one of the tasks were successful, the `callback` will be
|
---|
| 29 | * passed the result of the successful task. If all tasks fail, the callback
|
---|
| 30 | * will be passed the error and result (if any) of the final attempt.
|
---|
| 31 | *
|
---|
| 32 | * @name tryEach
|
---|
| 33 | * @static
|
---|
| 34 | * @memberOf module:ControlFlow
|
---|
| 35 | * @method
|
---|
| 36 | * @category Control Flow
|
---|
| 37 | * @param {Array|Iterable|Object} tasks - A collection containing functions to
|
---|
| 38 | * run, each function is passed a `callback(err, result)` it must call on
|
---|
| 39 | * completion with an error `err` (which can be `null`) and an optional `result`
|
---|
| 40 | * value.
|
---|
| 41 | * @param {Function} [callback] - An optional callback which is called when one
|
---|
| 42 | * of the tasks has succeeded, or all have failed. It receives the `err` and
|
---|
| 43 | * `result` arguments of the last attempt at completing the `task`. Invoked with
|
---|
| 44 | * (err, results).
|
---|
| 45 | * @example
|
---|
| 46 | * async.tryEach([
|
---|
| 47 | * function getDataFromFirstWebsite(callback) {
|
---|
| 48 | * // Try getting the data from the first website
|
---|
| 49 | * callback(err, data);
|
---|
| 50 | * },
|
---|
| 51 | * function getDataFromSecondWebsite(callback) {
|
---|
| 52 | * // First website failed,
|
---|
| 53 | * // Try getting the data from the backup website
|
---|
| 54 | * callback(err, data);
|
---|
| 55 | * }
|
---|
| 56 | * ],
|
---|
| 57 | * // optional callback
|
---|
| 58 | * function(err, results) {
|
---|
| 59 | * Now do something with the data.
|
---|
| 60 | * });
|
---|
| 61 | *
|
---|
| 62 | */
|
---|
| 63 | function tryEach(tasks, callback) {
|
---|
| 64 | var error = null;
|
---|
| 65 | var result;
|
---|
| 66 | callback = callback || _noop2.default;
|
---|
| 67 | (0, _eachSeries2.default)(tasks, function (task, callback) {
|
---|
| 68 | (0, _wrapAsync2.default)(task)(function (err, res /*, ...args*/) {
|
---|
| 69 | if (arguments.length > 2) {
|
---|
| 70 | result = (0, _slice2.default)(arguments, 1);
|
---|
| 71 | } else {
|
---|
| 72 | result = res;
|
---|
| 73 | }
|
---|
| 74 | error = err;
|
---|
| 75 | callback(!err);
|
---|
| 76 | });
|
---|
| 77 | }, function () {
|
---|
| 78 | callback(error, result);
|
---|
| 79 | });
|
---|
| 80 | }
|
---|
| 81 | module.exports = exports['default']; |
---|