retryable.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. return (0, _initialParams2.default)(function (args, callback) {
  11. function taskFn(cb) {
  12. task.apply(null, args.concat([cb]));
  13. }
  14. if (opts) (0, _retry2.default)(opts, taskFn, callback);else (0, _retry2.default)(taskFn, callback);
  15. });
  16. };
  17. var _retry = require('./retry');
  18. var _retry2 = _interopRequireDefault(_retry);
  19. var _initialParams = require('./internal/initialParams');
  20. var _initialParams2 = _interopRequireDefault(_initialParams);
  21. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  22. module.exports = exports['default'];
  23. /**
  24. * A close relative of [`retry`]{@link module:ControlFlow.retry}. This method wraps a task and makes it
  25. * retryable, rather than immediately calling it with retries.
  26. *
  27. * @name retryable
  28. * @static
  29. * @memberOf module:ControlFlow
  30. * @method
  31. * @see [async.retry]{@link module:ControlFlow.retry}
  32. * @category Control Flow
  33. * @param {Object|number} [opts = {times: 5, interval: 0}| 5] - optional
  34. * options, exactly the same as from `retry`
  35. * @param {Function} task - the asynchronous function to wrap
  36. * @returns {Functions} The wrapped function, which when invoked, will retry on
  37. * an error, based on the parameters specified in `opts`.
  38. * @example
  39. *
  40. * async.auto({
  41. * dep1: async.retryable(3, getFromFlakyService),
  42. * process: ["dep1", async.retryable(3, function (results, cb) {
  43. * maybeProcessData(results.dep1, cb);
  44. * })]
  45. * }, callback);
  46. */