Skip to content

Instantly share code, notes, and snippets.

@randyburden
Created March 10, 2022 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randyburden/ae2e3e31552af8e8752ecac47edc7389 to your computer and use it in GitHub Desktop.
Save randyburden/ae2e3e31552af8e8752ecac47edc7389 to your computer and use it in GitHub Desktop.
JavaScript retry helper utility that retries a function until the function returns a truthy value or the timeout elapses.
/*
Retry helper utility
Author: Randy Burden - 2022 - https://www.randyburden.com/
Dependencies: none
License:
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
// global _: false
var utils = utils || {};
utils.retryHelper = new function () {
function isFunction(func) {
return Object.prototype.toString.call(func) === '[object Function]';
};
/**
* Retries calling a function until it returns a truthy value or the timeout elapses.
* @param {Object} options (Required) Retry options.
* @param {number} options.timeOut (Required) Timeout in milliseconds. Once the timeout elapses, it will stop retrying.
* @param {number} options.retryInterval (Required) The interval/delay between retry attempts in milliseconds.
* @param {function} options.retry (Required) The function to retry until it returns a truthy value or the timeout elapses.
* @param {function} options.success (Optional) The function called when the retry function returns a truthy value.
* @param {function} options.fail (Optional) The function called when the timeout elapses.
*/
this.retry = function (options) {
var timeOut = options.timeOut;
var retryInterval = options.retryInterval;
var retry = options.retry;
var success = options.success;
var fail = options.fail;
var startDate = Date.now();
var endDate = Date.now() + timeOut;
var iterations = 0;
function retryHandler() {
iterations++;
if (Date.now() > endDate) {
if (isFunction(fail)) {
fail({
result: null,
iterations: iterations,
elapsed: Date.now() - startDate
});
}
return;
}
var result = retry({
iterations: iterations,
elapsed: Date.now() - startDate
});
if (result) {
if (isFunction(success)) {
success({
result: result,
iterations: iterations,
elapsed: Date.now() - startDate
});
}
return;
}
setTimeout(retryHandler, retryInterval);
}
retryHandler();
};
};
/*
Example where the success function is called immediately
Console output: { result: true, iterations: 1, duration: 0 }
*/
utils.retryHelper.retry({
timeOut: 1000,
retryInterval: 100,
retry: function(e) {
return true;
},
success: function(e) {
console.log(e);
},
fail: function(e) {
console.error(e);
}
});
/*
Example where the success function is called after 3 iterations/attempts
Console output: { result: true, iterations: 3, duration: 2318 }
*/
utils.retryHelper.retry({
timeOut: 5000,
retryInterval: 1000,
retry: function(e) {
return e.iterations === 3 ? true : false;
},
success: function(e) {
console.log(e);
},
fail: function(e) {
console.error(e);
}
});
/*
Example where the fail function is called
Console output: { result: null, iterations: 6, duration: 5201 }
*/
utils.retryHelper.retry({
timeOut: 5000,
retryInterval: 1000,
retry: function(e) {
return false;
},
success: function(e) {
console.log(e);
},
fail: function(e) {
console.error(e);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment