Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

tryEach.js 2.5KB

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