Last active
August 12, 2017 21:33
-
-
Save greenygh0st/28b4997cf56441088da1a9504ec2b989 to your computer and use it in GitHub Desktop.
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
function exponentialBackoff(toTry, max, delay, callback) { | |
new Promise(function(resolve, reject) { | |
// do a thing then… | |
var ret = toTry.toString(); | |
ret = ret.substr('function '.length); | |
ret = ret.substr(0, ret.indexOf('(')); | |
console.log('EB '+ret+' max',max,'next delay',delay); | |
var result = toTry(); | |
if (result) { | |
resolve(true); | |
} else { | |
if (max > 0) { | |
setTimeout(function() { | |
exponentialBackoff(toTry, --max, delay * 2, callback); | |
}, delay); | |
} else { | |
console.log('and I\'m giving up now...'); | |
resolve(false); | |
} | |
} | |
}).then(function(result){ | |
callback(result); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment