Skip to content

Instantly share code, notes, and snippets.

@ycmjason
Last active March 20, 2019 15:32
Show Gist options
  • Save ycmjason/e54f201c95c727c44126f8d1634e9787 to your computer and use it in GitHub Desktop.
Save ycmjason/e54f201c95c727c44126f8d1634e9787 to your computer and use it in GitHub Desktop.
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