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