-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow custom retry interval #1161
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,16 +64,20 @@ export default function retry(times, task, callback) { | |
var DEFAULT_TIMES = 5; | ||
var DEFAULT_INTERVAL = 0; | ||
|
||
|
||
var opts = { | ||
times: DEFAULT_TIMES, | ||
interval: DEFAULT_INTERVAL | ||
intervalFunc: constantIntervalFunc(DEFAULT_INTERVAL) | ||
}; | ||
|
||
function parseTimes(acc, t) { | ||
if (typeof t === 'object') { | ||
acc.times = +t.times || DEFAULT_TIMES; | ||
acc.interval = +t.interval || DEFAULT_INTERVAL; | ||
|
||
if (typeof t.interval === 'function') { | ||
acc.intervalFunc = t.interval; | ||
} else { | ||
acc.intervalFunc = constantIntervalFunc(+t.interval || DEFAULT_INTERVAL); | ||
} | ||
} else if (typeof t === 'number' || typeof t === 'string') { | ||
acc.times = +t || DEFAULT_TIMES; | ||
} else { | ||
|
@@ -95,13 +99,13 @@ export default function retry(times, task, callback) { | |
throw new Error("Invalid arguments for async.retry"); | ||
} | ||
|
||
|
||
var attempts = []; | ||
while (opts.times) { | ||
var isFinalAttempt = !(opts.times -= 1); | ||
for (var i = 1; i < opts.times + 1; i++) { | ||
var isFinalAttempt = (i == opts.times); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why the change to this loop? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I needed to pass the retryCount to the interval function but the loop was counting backwards and destroying opts.times. |
||
attempts.push(retryAttempt(isFinalAttempt)); | ||
if (!isFinalAttempt && opts.interval > 0) { | ||
attempts.push(retryInterval(opts.interval)); | ||
var interval = opts.intervalFunc(i); | ||
if (!isFinalAttempt && interval > 0) { | ||
attempts.push(retryInterval(interval)); | ||
} | ||
} | ||
|
||
|
@@ -129,4 +133,10 @@ export default function retry(times, task, callback) { | |
}, interval); | ||
}; | ||
} | ||
|
||
function constantIntervalFunc(interval) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you |
||
return function() { | ||
return interval; | ||
}; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ternary this guy