A fork of https://github.com/ueokande/vim-vixen
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.
30 lines
593 B
30 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;
|
|
|