Created
August 9, 2019 20:13
-
-
Save rodolfobarretoweb/e0105244ad66194d624f1a54d9ac4826 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 attempt(fn, retrayDelay, maxAttempts) { | |
let loop = null; | |
let counter = 0; | |
return new Promise((resolve, reject) => { | |
try { | |
const resolve = fn(); | |
resolve(resolve); | |
} catch(error) { | |
loop = setTimeout(() => { | |
counter = counter + retrayDelay; | |
if(counter < maxAttempts) { | |
fn(); | |
} else { | |
reject(error); | |
} | |
}, retrayDelay); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment