Last change
on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.3 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var errcode = require('err-code');
|
---|
| 4 | var retry = require('retry');
|
---|
| 5 |
|
---|
| 6 | var hasOwn = Object.prototype.hasOwnProperty;
|
---|
| 7 |
|
---|
| 8 | function isRetryError(err) {
|
---|
| 9 | return err && err.code === 'EPROMISERETRY' && hasOwn.call(err, 'retried');
|
---|
| 10 | }
|
---|
| 11 |
|
---|
| 12 | function promiseRetry(fn, options) {
|
---|
| 13 | var temp;
|
---|
| 14 | var operation;
|
---|
| 15 |
|
---|
| 16 | if (typeof fn === 'object' && typeof options === 'function') {
|
---|
| 17 | // Swap options and fn when using alternate signature (options, fn)
|
---|
| 18 | temp = options;
|
---|
| 19 | options = fn;
|
---|
| 20 | fn = temp;
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | operation = retry.operation(options);
|
---|
| 24 |
|
---|
| 25 | return new Promise(function (resolve, reject) {
|
---|
| 26 | operation.attempt(function (number) {
|
---|
| 27 | Promise.resolve()
|
---|
| 28 | .then(function () {
|
---|
| 29 | return fn(function (err) {
|
---|
| 30 | if (isRetryError(err)) {
|
---|
| 31 | err = err.retried;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | throw errcode(new Error('Retrying'), 'EPROMISERETRY', { retried: err });
|
---|
| 35 | }, number);
|
---|
| 36 | })
|
---|
| 37 | .then(resolve, function (err) {
|
---|
| 38 | if (isRetryError(err)) {
|
---|
| 39 | err = err.retried;
|
---|
| 40 |
|
---|
| 41 | if (operation.retry(err || new Error())) {
|
---|
| 42 | return;
|
---|
| 43 | }
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | reject(err);
|
---|
| 47 | });
|
---|
| 48 | });
|
---|
| 49 | });
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | module.exports = promiseRetry;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.