async-rerun
Version:
Perfect light-weight & asynchronious package for asynchroniously retry/rerun of any logic...
10 lines (9 loc) • 1.33 kB
JavaScript
/**
* @param {Function} fn The function which will be used as parameter should return a PROMISE & SHOULD NOT HAVE ANY PARAMETER it's compulsory, as its a async retry mechanism.
* @param {Number} maxRetries Max Retries in number.
* @param {Number} delayInMS Delay time for retries in number as mili-seconds(ms).
* @param {Boolean} alwaysRetry To retry always untill the positive response, If its true then maxRetries will not count.
* @param {Boolean} showLogs To show logs for all retries.
*/
module.exports.runAsync=(fn,maxRetries,delayInMS,alwaysRetry=!1,showLogs=!1)=>new Promise((l,a)=>{function k(){fn().then(c=>{l(c)}).catch(c=>{e++;e<=maxRetries||alwaysRetry?(showLogs&&(alwaysRetry?console.log(`Retry ${e} in ${delayInMS}ms due to error:`,c):console.log(`Retry ${e} of ${maxRetries} in ${delayInMS}ms due to error:`,c)),setTimeout(k,delayInMS)):a(c)})}if("number"!=typeof maxRetries)a("maxRetries should be in number type.");else if("number"!=typeof delayInMS)a("delayInMS should be in number type.");else if("boolean"!=typeof showLogs)a("showLogs should be in boolean type.");else if("function"!=typeof fn)a("fn should be in function type.");
else if(0>maxRetries)a("maxRetries should be greater than or equal to zero.");else if(0>delayInMS)a("delayInMS should be greater than or equal to zero.");else{var e=0;k()}});