Skip to content

Instantly share code, notes, and snippets.

@mebubo
Created June 29, 2020 15:04
Show Gist options
  • Save mebubo/634f12b9ed155307f60f52372eee5d8e to your computer and use it in GitHub Desktop.
Save mebubo/634f12b9ed155307f60f52372eee5d8e to your computer and use it in GitHub Desktop.
type IO<A> = () => Promise<A>
function retry<A>(x: IO<A>, p: (a: A) => Boolean, retries: number): IO<A> {
return () => {
if (retries <= 0) {
return x()
}
return x().then(xx => {
if (p(xx)) {
return Promise.resolve(xx)
} else {
return retry(x, p, retries - 1)()
}
})
}
}
retry(() => {
console.log(".")
return fetch("https://httpbin.org/delay/1")
}, () => false, 5)()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment