Skip to content

Instantly share code, notes, and snippets.

@kruschid
Created November 14, 2019 16:27
Show Gist options
  • Save kruschid/9652519c5567d259543a0ac9667b7531 to your computer and use it in GitHub Desktop.
Save kruschid/9652519c5567d259543a0ac9667b7531 to your computer and use it in GitHub Desktop.
eventually
const eventually = <T extends any>(
cb: () => Promise<T>,
timeout = 20000,
interval = 1000
): Promise<T> =>
timeout <= 0
? Promise.reject()
: cb().catch(() =>
new Promise((resolve) => {
setTimeout(
() => resolve(eventually(cb, timeout - interval, interval)),
interval
);
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment