3 Object.defineProperty(exports, "__esModule", {
6 exports.default = retry;
8 var _noop = require('lodash/noop');
10 var _noop2 = _interopRequireDefault(_noop);
12 var _constant = require('lodash/constant');
14 var _constant2 = _interopRequireDefault(_constant);
16 function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
19 * Attempts to get a successful response from `task` no more than `times` times
20 * before returning an error. If the task is successful, the `callback` will be
21 * passed the result of the successful task. If all attempts fail, the callback
22 * will be passed the error and result (if any) of the final attempt.
26 * @memberOf module:ControlFlow
28 * @category Control Flow
29 * @param {Object|number} [opts = {times: 5, interval: 0}| 5] - Can be either an
30 * object with `times` and `interval` or a number.
31 * * `times` - The number of attempts to make before giving up. The default
33 * * `interval` - The time to wait between retries, in milliseconds. The
34 * default is `0`. The interval may also be specified as a function of the
35 * retry count (see example).
36 * * `errorFilter` - An optional synchronous function that is invoked on
37 * erroneous result. If it returns `true` the retry attempts will continue;
38 * if the function returns `false` the retry flow is aborted with the current
39 * attempt's error and result being returned to the final callback.
41 * * If `opts` is a number, the number specifies the number of times to retry,
42 * with the default interval of `0`.
43 * @param {Function} task - A function which receives two arguments: (1) a
44 * `callback(err, result)` which must be called when finished, passing `err`
45 * (which can be `null`) and the `result` of the function's execution, and (2)
46 * a `results` object, containing the results of the previously executed
47 * functions (if nested inside another control flow). Invoked with
48 * (callback, results).
49 * @param {Function} [callback] - An optional callback which is called when the
50 * task has succeeded, or after the final failed attempt. It receives the `err`
51 * and `result` arguments of the last attempt at completing the `task`. Invoked
52 * with (err, results).
55 * // The `retry` function can be used as a stand-alone control flow by passing
56 * // a callback, as shown below:
58 * // try calling apiMethod 3 times
59 * async.retry(3, apiMethod, function(err, result) {
60 * // do something with the result
63 * // try calling apiMethod 3 times, waiting 200 ms between each retry
64 * async.retry({times: 3, interval: 200}, apiMethod, function(err, result) {
65 * // do something with the result
68 * // try calling apiMethod 10 times with exponential backoff
69 * // (i.e. intervals of 100, 200, 400, 800, 1600, ... milliseconds)
72 * interval: function(retryCount) {
73 * return 50 * Math.pow(2, retryCount);
75 * }, apiMethod, function(err, result) {
76 * // do something with the result
79 * // try calling apiMethod the default 5 times no delay between each retry
80 * async.retry(apiMethod, function(err, result) {
81 * // do something with the result
84 * // try calling apiMethod only when error condition satisfies, all other
85 * // errors will abort the retry control flow and return to final callback
87 * errorFilter: function(err) {
88 * return err.message === 'Temporary error'; // only retry on a specific error
90 * }, apiMethod, function(err, result) {
91 * // do something with the result
94 * // It can also be embedded within other control flow functions to retry
95 * // individual methods that are not as reliable, like this:
97 * users: api.getUsers.bind(api),
98 * payments: async.retry(3, api.getPayments.bind(api))
99 * }, function(err, results) {
100 * // do something with the results
104 function retry(opts, task, callback) {
105 var DEFAULT_TIMES = 5;
106 var DEFAULT_INTERVAL = 0;
109 times: DEFAULT_TIMES,
110 intervalFunc: (0, _constant2.default)(DEFAULT_INTERVAL)
113 function parseTimes(acc, t) {
114 if (typeof t === 'object') {
115 acc.times = +t.times || DEFAULT_TIMES;
117 acc.intervalFunc = typeof t.interval === 'function' ? t.interval : (0, _constant2.default)(+t.interval || DEFAULT_INTERVAL);
119 acc.errorFilter = t.errorFilter;
120 } else if (typeof t === 'number' || typeof t === 'string') {
121 acc.times = +t || DEFAULT_TIMES;
123 throw new Error("Invalid arguments for async.retry");
127 if (arguments.length < 3 && typeof opts === 'function') {
128 callback = task || _noop2.default;
131 parseTimes(options, opts);
132 callback = callback || _noop2.default;
135 if (typeof task !== 'function') {
136 throw new Error("Invalid arguments for async.retry");
140 function retryAttempt() {
141 task(function (err) {
142 if (err && attempt++ < options.times && (typeof options.errorFilter != 'function' || options.errorFilter(err))) {
143 setTimeout(retryAttempt, options.intervalFunc(attempt));
145 callback.apply(null, arguments);
152 module.exports = exports['default'];