source: trip-planner-front/node_modules/async/tryEach.js@ 76712b2

Last change on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.5 KB
Line 
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.default = tryEach;
7
8var _noop = require('lodash/noop');
9
10var _noop2 = _interopRequireDefault(_noop);
11
12var _eachSeries = require('./eachSeries');
13
14var _eachSeries2 = _interopRequireDefault(_eachSeries);
15
16var _wrapAsync = require('./internal/wrapAsync');
17
18var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
19
20var _slice = require('./internal/slice');
21
22var _slice2 = _interopRequireDefault(_slice);
23
24function _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 */
63function 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}
81module.exports = exports['default'];
Note: See TracBrowser for help on using the repository browser.