promise-again
Advanced tools
Comparing version 1.1.0 to 1.2.0
export interface IOptions { | ||
delay?: number | ((attempt: number, ...args: any[]) => number); | ||
attempts: number | ((attempt: number, ...args: any[]) => boolean); | ||
retryArgumentsInterceptor?: (attempt: number, ...args: any[]) => any[]; | ||
onCatch?: (reason: any, attempt: number, ...args: any[]) => void; | ||
retryArgumentsInterceptor?: (reason: any, attempt: number, ...args: any[]) => any[]; | ||
} | ||
export default function promiseAgain<T>(func: (...args: any[]) => Promise<T>, options: IOptions): (...args: any[]) => Promise<T>; |
@@ -16,7 +16,4 @@ import * as promiseDelay from 'delay'; | ||
usedAttempts += 1; | ||
if (options.onCatch) { | ||
options.onCatch.apply(options, [reason, usedAttempts].concat(innerArgs)); | ||
} | ||
var newArguments = options.retryArgumentsInterceptor ? | ||
(options.retryArgumentsInterceptor.apply(options, [usedAttempts].concat(innerArgs)) || []) : innerArgs; | ||
(options.retryArgumentsInterceptor.apply(options, [reason, usedAttempts].concat(innerArgs)) || []) : innerArgs; | ||
var shouldRetry = false; | ||
@@ -23,0 +20,0 @@ if (typeof options.attempts === 'number') { |
export interface IOptions { | ||
delay?: number | ((attempt: number, ...args: any[]) => number); | ||
attempts: number | ((attempt: number, ...args: any[]) => boolean); | ||
retryArgumentsInterceptor?: (attempt: number, ...args: any[]) => any[]; | ||
onCatch?: (reason: any, attempt: number, ...args: any[]) => void; | ||
retryArgumentsInterceptor?: (reason: any, attempt: number, ...args: any[]) => any[]; | ||
} | ||
export default function promiseAgain<T>(func: (...args: any[]) => Promise<T>, options: IOptions): (...args: any[]) => Promise<T>; |
@@ -27,7 +27,4 @@ (function (factory) { | ||
usedAttempts += 1; | ||
if (options.onCatch) { | ||
options.onCatch.apply(options, [reason, usedAttempts].concat(innerArgs)); | ||
} | ||
var newArguments = options.retryArgumentsInterceptor ? | ||
(options.retryArgumentsInterceptor.apply(options, [usedAttempts].concat(innerArgs)) || []) : innerArgs; | ||
(options.retryArgumentsInterceptor.apply(options, [reason, usedAttempts].concat(innerArgs)) || []) : innerArgs; | ||
var shouldRetry = false; | ||
@@ -34,0 +31,0 @@ if (typeof options.attempts === 'number') { |
@@ -52,21 +52,2 @@ import {expect} from 'chai'; | ||
it('should call retryArgumentsInterceptor for attempts count and original arguments', (done) => { | ||
const func = sinon.stub(); | ||
func.onCall(0).rejects('Some reason 1'); | ||
func.onCall(1).rejects('Some reason 2'); | ||
func.onCall(2).resolves('Needed value'); | ||
const retryArgumentsInterceptor = sinon.stub(); | ||
retryArgumentsInterceptor.returns([1, 2, 3, 'some arg']); | ||
const composedFunction = promiseAgain(func, {attempts: 5, retryArgumentsInterceptor}); | ||
composedFunction(1, 2, 3, 'some arg').then(() => { | ||
expect(retryArgumentsInterceptor.callCount).to.equal(2); | ||
expect(retryArgumentsInterceptor.calledWithExactly(1, 1, 2, 3, 'some arg')).to.equal(true); | ||
expect(retryArgumentsInterceptor.calledWithExactly(2, 1, 2, 3, 'some arg')).to.equal(true); | ||
done(); | ||
}); | ||
}); | ||
it('should call the original function with result of retryArgumentsInterceptor', (done) => { | ||
@@ -107,4 +88,15 @@ const func = sinon.stub(); | ||
expect(retryArgumentsInterceptor.callCount).to.equal(2); | ||
expect(retryArgumentsInterceptor.calledWithExactly(1, 1, 2, 3, 'some arg')).to.equal(true); | ||
expect(retryArgumentsInterceptor.calledWithExactly(2, 5, 6, 7)).to.equal(true); | ||
expect( | ||
retryArgumentsInterceptor.calledWithExactly( | ||
sinon.match({name: 'Some reason 1'}), 1, 1, 2, 3, 'some arg', | ||
), | ||
).to.equal(true); | ||
expect( | ||
retryArgumentsInterceptor.calledWithExactly( | ||
sinon.match({name: 'Some reason 2'}), 2, 5, 6, 7, | ||
), | ||
).to.equal(true); | ||
done(); | ||
@@ -250,26 +242,2 @@ }); | ||
}); | ||
it('should call onCatch with catch reason, attempts count and original arguments', (done) => { | ||
const func = sinon.stub(); | ||
func.onCall(0).rejects('Some reason 1'); | ||
func.onCall(1).rejects('Some reason 2'); | ||
func.onCall(2).resolves('Needed value'); | ||
const onCatch = sinon.spy(); | ||
const composedFunction = promiseAgain(func, {attempts: 5, onCatch}); | ||
composedFunction(1, 2, 3, 'some arg').then(() => { | ||
expect(onCatch.callCount).to.equal(2); | ||
expect( | ||
onCatch.calledWithExactly(sinon.match({name: 'Some reason 1'}), 1, 1, 2, 3, 'some arg'), | ||
).to.equal(true); | ||
expect( | ||
onCatch.calledWithExactly(sinon.match({name: 'Some reason 2'}), 2, 1, 2, 3, 'some arg'), | ||
).to.equal(true); | ||
done(); | ||
}); | ||
}); | ||
}); |
@@ -6,4 +6,3 @@ import * as promiseDelay from 'delay'; | ||
attempts: number | ((attempt: number, ...args: any[]) => boolean); | ||
retryArgumentsInterceptor?: (attempt: number, ...args: any[]) => any[]; | ||
onCatch?: (reason: any, attempt: number, ...args: any[]) => void; | ||
retryArgumentsInterceptor?: (reason: any, attempt: number, ...args: any[]) => any[]; | ||
} | ||
@@ -22,8 +21,4 @@ | ||
if (options.onCatch) { | ||
options.onCatch(reason, usedAttempts, ...innerArgs); | ||
} | ||
const newArguments = options.retryArgumentsInterceptor ? | ||
(options.retryArgumentsInterceptor(usedAttempts, ...innerArgs) || []) : innerArgs; | ||
(options.retryArgumentsInterceptor(reason, usedAttempts, ...innerArgs) || []) : innerArgs; | ||
@@ -30,0 +25,0 @@ let shouldRetry = false; |
{ | ||
"name": "promise-again", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"main": "./dist/umd/index.js", | ||
@@ -5,0 +5,0 @@ "module": "./dist/es6/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
47197
376