Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

retry.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = retry;
  6. var _wrapAsync = require('./internal/wrapAsync');
  7. var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
  8. var _promiseCallback = require('./internal/promiseCallback');
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. function constant(value) {
  11. return function () {
  12. return value;
  13. };
  14. }
  15. /**
  16. * Attempts to get a successful response from `task` no more than `times` times
  17. * before returning an error. If the task is successful, the `callback` will be
  18. * passed the result of the successful task. If all attempts fail, the callback
  19. * will be passed the error and result (if any) of the final attempt.
  20. *
  21. * @name retry
  22. * @static
  23. * @memberOf module:ControlFlow
  24. * @method
  25. * @category Control Flow
  26. * @see [async.retryable]{@link module:ControlFlow.retryable}
  27. * @param {Object|number} [opts = {times: 5, interval: 0}| 5] - Can be either an
  28. * object with `times` and `interval` or a number.
  29. * * `times` - The number of attempts to make before giving up. The default
  30. * is `5`.
  31. * * `interval` - The time to wait between retries, in milliseconds. The
  32. * default is `0`. The interval may also be specified as a function of the
  33. * retry count (see example).
  34. * * `errorFilter` - An optional synchronous function that is invoked on
  35. * erroneous result. If it returns `true` the retry attempts will continue;
  36. * if the function returns `false` the retry flow is aborted with the current
  37. * attempt's error and result being returned to the final callback.
  38. * Invoked with (err).
  39. * * If `opts` is a number, the number specifies the number of times to retry,
  40. * with the default interval of `0`.
  41. * @param {AsyncFunction} task - An async function to retry.
  42. * Invoked with (callback).
  43. * @param {Function} [callback] - An optional callback which is called when the
  44. * task has succeeded, or after the final failed attempt. It receives the `err`
  45. * and `result` arguments of the last attempt at completing the `task`. Invoked
  46. * with (err, results).
  47. * @returns {Promise} a promise if no callback provided
  48. *
  49. * @example
  50. *
  51. * // The `retry` function can be used as a stand-alone control flow by passing
  52. * // a callback, as shown below:
  53. *
  54. * // try calling apiMethod 3 times
  55. * async.retry(3, apiMethod, function(err, result) {
  56. * // do something with the result
  57. * });
  58. *
  59. * // try calling apiMethod 3 times, waiting 200 ms between each retry
  60. * async.retry({times: 3, interval: 200}, apiMethod, function(err, result) {
  61. * // do something with the result
  62. * });
  63. *
  64. * // try calling apiMethod 10 times with exponential backoff
  65. * // (i.e. intervals of 100, 200, 400, 800, 1600, ... milliseconds)
  66. * async.retry({
  67. * times: 10,
  68. * interval: function(retryCount) {
  69. * return 50 * Math.pow(2, retryCount);
  70. * }
  71. * }, apiMethod, function(err, result) {
  72. * // do something with the result
  73. * });
  74. *
  75. * // try calling apiMethod the default 5 times no delay between each retry
  76. * async.retry(apiMethod, function(err, result) {
  77. * // do something with the result
  78. * });
  79. *
  80. * // try calling apiMethod only when error condition satisfies, all other
  81. * // errors will abort the retry control flow and return to final callback
  82. * async.retry({
  83. * errorFilter: function(err) {
  84. * return err.message === 'Temporary error'; // only retry on a specific error
  85. * }
  86. * }, apiMethod, function(err, result) {
  87. * // do something with the result
  88. * });
  89. *
  90. * // to retry individual methods that are not as reliable within other
  91. * // control flow functions, use the `retryable` wrapper:
  92. * async.auto({
  93. * users: api.getUsers.bind(api),
  94. * payments: async.retryable(3, api.getPayments.bind(api))
  95. * }, function(err, results) {
  96. * // do something with the results
  97. * });
  98. *
  99. */
  100. const DEFAULT_TIMES = 5;
  101. const DEFAULT_INTERVAL = 0;
  102. function retry(opts, task, callback) {
  103. var options = {
  104. times: DEFAULT_TIMES,
  105. intervalFunc: constant(DEFAULT_INTERVAL)
  106. };
  107. if (arguments.length < 3 && typeof opts === 'function') {
  108. callback = task || (0, _promiseCallback.promiseCallback)();
  109. task = opts;
  110. } else {
  111. parseTimes(options, opts);
  112. callback = callback || (0, _promiseCallback.promiseCallback)();
  113. }
  114. if (typeof task !== 'function') {
  115. throw new Error("Invalid arguments for async.retry");
  116. }
  117. var _task = (0, _wrapAsync2.default)(task);
  118. var attempt = 1;
  119. function retryAttempt() {
  120. _task((err, ...args) => {
  121. if (err === false) return;
  122. if (err && attempt++ < options.times && (typeof options.errorFilter != 'function' || options.errorFilter(err))) {
  123. setTimeout(retryAttempt, options.intervalFunc(attempt - 1));
  124. } else {
  125. callback(err, ...args);
  126. }
  127. });
  128. }
  129. retryAttempt();
  130. return callback[_promiseCallback.PROMISE_SYMBOL];
  131. }
  132. function parseTimes(acc, t) {
  133. if (typeof t === 'object') {
  134. acc.times = +t.times || DEFAULT_TIMES;
  135. acc.intervalFunc = typeof t.interval === 'function' ? t.interval : constant(+t.interval || DEFAULT_INTERVAL);
  136. acc.errorFilter = t.errorFilter;
  137. } else if (typeof t === 'number' || typeof t === 'string') {
  138. acc.times = +t || DEFAULT_TIMES;
  139. } else {
  140. throw new Error("Invalid arguments for async.retry");
  141. }
  142. }
  143. module.exports = exports['default'];