Last active
March 20, 2019 15:32
-
-
Save ycmjason/e54f201c95c727c44126f8d1634e9787 to your computer and use it in GitHub Desktop.
Retry async functions with timeout
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
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