node-fetch-retry
Advanced tools
Comparing version 1.0.1 to 1.1.0
17
index.js
@@ -5,2 +5,4 @@ const fetch = require('node-fetch'); | ||
let retry = opts && opts.retry || 3 | ||
let pause = (opts && opts.pause && opts.pause > 0) || 0 | ||
while (retry > 0) { | ||
@@ -12,3 +14,3 @@ try { | ||
opts.callback(retry) | ||
} | ||
} | ||
retry = retry - 1 | ||
@@ -18,4 +20,15 @@ if (retry == 0) { | ||
} | ||
if (opts.pause) { | ||
console.log("pausing.."); | ||
await sleep(opts.pause); | ||
console.log("done pausing..."); | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
function sleep(ms) { | ||
return new Promise(resolve => setTimeout(resolve, ms)); | ||
} |
{ | ||
"name": "node-fetch-retry", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Retry library for node-fetch", | ||
@@ -26,8 +26,8 @@ "scripts": { | ||
"devDependencies": { | ||
"mocha": "^5.2.0", | ||
"mocha": "^7.0.0", | ||
"should": "^13.2.3" | ||
}, | ||
"dependencies": { | ||
"node-fetch": "^2.2.0" | ||
"node-fetch": "^2.6.1" | ||
} | ||
} |
@@ -27,5 +27,14 @@ # node-fetch-retry [![npm version](https://img.shields.io/npm/v/node-fetch-retry.svg?style=flat)](https://www.npmjs.com/package/node-fetch-retry) [![NPM](https://img.shields.io/npm/dt/node-fetch-retry.svg?style=flat-square&colorB=fd7463)](https://www.npmjs.com/package/node-fetch-retry) <a href="https://travis-ci.org/greatjapa/node-fetch-retry"><img alt="Travis Status" src="https://travis-ci.org/greatjapa/node-fetch-retry.svg?branch=master"></a> [![license](https://img.shields.io/github/license/mashape/apistatus.svg?maxAge=2592000)](https://github.com/greatjapa/node-fetch-retry/blob/master/LICENSE) | ||
If you want to add callback that will be called between the retries: | ||
A pause (in milliseconds) can be added between retry attempts using the `pause` option. Pause values < 0 are treated as 0. | ||
The following example waits 1 seconds (1000 ms) between retry attempts. | ||
```javascript | ||
const fetch = require('node-fetch-retry'); | ||
let res = await fetch('https://google.com', { method: 'GET', retry: 3, pause: 1000 }) | ||
``` | ||
If you want to add callback that will be called between the retries. The callback is invoked BEFORE any (optional) pauses. | ||
```javascript | ||
let opts = { | ||
@@ -32,0 +41,0 @@ method: 'GET', |
@@ -54,4 +54,2 @@ require('should'); | ||
it('should raise an error because could not find URL', async function () { | ||
this.timeout(10000); | ||
let calls = [] | ||
@@ -71,2 +69,21 @@ let opts = { | ||
}); | ||
it('should work properly with pause', async function () { | ||
this.timeout(10000); | ||
let calls = [] | ||
let opts = { | ||
retry: 3, | ||
pause: 1000, // in milliseconds | ||
callback: retry => { | ||
calls.push(new Date().getTime()) | ||
} | ||
} | ||
try { | ||
await fetch('https://fakeURLfake', opts) | ||
} catch (e) { | ||
should.equal(true, calls[1] - calls[0] >= 1000); | ||
should.equal(true, calls[2] - calls[1] >= 1000); | ||
} | ||
}); | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6818
102
47
6
Updatednode-fetch@^2.6.1