A function that keeps trying, "fn" until it returns true or has tried "max" number of times. First retry has a delay of "delay". "callback" is called upon success.
Last active
April 15, 2019 14:14
-
-
Save oleg-koval/75494a2f19bc240d7b04ed2596d738bd to your computer and use it in GitHub Desktop.
exponentialBackoff function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exponentialBackoff(fnFailsSomeTimes, 10, 100, function(result) { | |
console.log("the result is", result); | |
}); | |
function exponentialBackoff(fn, max, delay, callback) { | |
const result = fn(); | |
if (result) { | |
callback(result); | |
} else { | |
if (max > 0) { | |
setTimeout(function() { | |
exponentialBackoff(fn, --max, delay * 2, callback); | |
}, delay); | |
} else { | |
console.log("Retry count exceeded. Stopping calls"); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment