Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. 'use strict';
  2. var errcode = require('err-code');
  3. var retry = require('retry');
  4. var hasOwn = Object.prototype.hasOwnProperty;
  5. function isRetryError(err) {
  6. return err && err.code === 'EPROMISERETRY' && hasOwn.call(err, 'retried');
  7. }
  8. function promiseRetry(fn, options) {
  9. var temp;
  10. var operation;
  11. if (typeof fn === 'object' && typeof options === 'function') {
  12. // Swap options and fn when using alternate signature (options, fn)
  13. temp = options;
  14. options = fn;
  15. fn = temp;
  16. }
  17. operation = retry.operation(options);
  18. return new Promise(function (resolve, reject) {
  19. operation.attempt(function (number) {
  20. Promise.resolve()
  21. .then(function () {
  22. return fn(function (err) {
  23. if (isRetryError(err)) {
  24. err = err.retried;
  25. }
  26. throw errcode(new Error('Retrying'), 'EPROMISERETRY', { retried: err });
  27. }, number);
  28. })
  29. .then(resolve, function (err) {
  30. if (isRetryError(err)) {
  31. err = err.retried;
  32. if (operation.retry(err || new Error())) {
  33. return;
  34. }
  35. }
  36. reject(err);
  37. });
  38. });
  39. });
  40. }
  41. module.exports = promiseRetry;