Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Retry async functions with timeout
const retryAsyncWithTimeout = (fns, ms) => new Promise((res, rej) => {
let hasTimeout = false;
const retryAsyncUntilSucceed = fn => new Promise((res, rej) => {
fn().then(res).catch(() => {
if (!hasTimeout) {
retryAsyncUntilSucceed(fn)
}
});
});
const ps = fns.map(fn => retryAsyncUntilSucceed(fn));
Promise.all(ps).then(res);
setTimeout(() => {
hasTimeout = true;
rej()
}, ms);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.