retry-machine
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -1,3 +0,6 @@ | ||
"use strict"; | ||
'use strict'; | ||
const nullLogger = require('null-logger'); | ||
const wait = ms => new Promise(resolve => setTimeout(() => { | ||
@@ -9,9 +12,11 @@ resolve(); | ||
function retry({ max = 3, delay = 1000, factor = 1 } = {}, failure = async () => {}) { | ||
return async function (promise, ...args) { | ||
function retry({ max = 3, delay = 1000, factor = 1, logger = nullLogger } = {}, failure = async () => {}) { | ||
return async function closure(promise, ...args) { | ||
let attempts = max || 3; | ||
let result = null; | ||
/* eslint-disable no-await-in-loop */ | ||
while (attempts > 0) { | ||
try { | ||
logger.info('Retry Machine: ', ...args); | ||
result = await promise(...args); | ||
@@ -18,0 +23,0 @@ break; |
{ | ||
"name": "retry-machine", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Retry Promises", | ||
@@ -24,2 +24,3 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"null-logger": "^1.0.0" | ||
}, | ||
@@ -26,0 +27,0 @@ "devDependencies": { |
@@ -12,4 +12,28 @@ # Retry Machine | ||
const runner = retry({ max: 5, delay: 1000, factor: 2 }); | ||
async function failure(e, attempt) { | ||
console.error(e); | ||
} | ||
const runner = retry({ max: 5, delay: 1000, factor: 2 }, failure); | ||
await runner(run, 1); | ||
``` | ||
``` | ||
--- | ||
Optionally pass in a logger: | ||
```javascript | ||
const { retry } = require('retry-machine'); | ||
const logger = require('./logger'); | ||
async function run(count) { | ||
console.log(count); | ||
} | ||
async function failure(e, attempt) { | ||
console.error(e); | ||
} | ||
const runner = retry({ max: 5, delay: 1000, factor: 2, logger }, failure); | ||
await runner(run, 1); | ||
``` |
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
3332
34
39
1
+ Addednull-logger@^1.0.0
+ Addednull-logger@1.0.0(transitive)