retryable.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = function (opts, task) {
  6. if (!task) {
  7. task = opts;
  8. opts = null;
  9. }
  10. var _task = (0, _wrapAsync2.default)(task);
  11. return (0, _initialParams2.default)(function (args, callback) {
  12. function taskFn(cb) {
  13. _task.apply(null, args.concat(cb));
  14. }
  15. if (opts) (0, _retry2.default)(opts, taskFn, callback);else (0, _retry2.default)(taskFn, callback);
  16. });
  17. };
  18. var _retry = require('./retry');
  19. var _retry2 = _interopRequireDefault(_retry);
  20. var _initialParams = require('./internal/initialParams');
  21. var _initialParams2 = _interopRequireDefault(_initialParams);
  22. var _wrapAsync = require('./internal/wrapAsync');
  23. var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
  24. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  25. module.exports = exports['default'];
  26. /**
  27. * A close relative of [`retry`]{@link module:ControlFlow.retry}. This method
  28. * wraps a task and makes it retryable, rather than immediately calling it
  29. * with retries.
  30. *
  31. * @name retryable
  32. * @static
  33. * @memberOf module:ControlFlow
  34. * @method
  35. * @see [async.retry]{@link module:ControlFlow.retry}
  36. * @category Control Flow
  37. * @param {Object|number} [opts = {times: 5, interval: 0}| 5] - optional
  38. * options, exactly the same as from `retry`
  39. * @param {AsyncFunction} task - the asynchronous function to wrap.
  40. * This function will be passed any arguments passed to the returned wrapper.
  41. * Invoked with (...args, callback).
  42. * @returns {AsyncFunction} The wrapped function, which when invoked, will
  43. * retry on an error, based on the parameters specified in `opts`.
  44. * This function will accept the same parameters as `task`.
  45. * @example
  46. *
  47. * async.auto({
  48. * dep1: async.retryable(3, getFromFlakyService),
  49. * process: ["dep1", async.retryable(3, function (results, cb) {
  50. * maybeProcessData(results.dep1, cb);
  51. * })]
  52. * }, callback);
  53. */