You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

31 lines
593 B

const defaultInterval = 100;
const defaultTimeout = 2000;
type Handler = () => void
const sleep = (ms: number): Promise<void> => {
return new Promise(resolve => setTimeout(resolve, ms));
}
const eventually = async (
fn: Handler,
timeout = defaultTimeout,
interval = defaultInterval,
): Promise<void> => {
let start = Date.now();
let loop = async() => {
try {
await fn();
} catch (err) {
if (Date.now() - start > timeout) {
throw err;
}
await sleep(interval);
await loop();
}
};
await loop();
};
export default eventually;