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.
24 lines
543 B
24 lines
543 B
6 years ago
|
let defaultInterval = 100;
|
||
|
let defaultTimeout = 2000;
|
||
|
|
||
|
function sleep(ms) {
|
||
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||
|
}
|
||
|
|
||
|
const eventually = async (fn, timeout = defaultTimeout, interval = defaultInterval) => {
|
||
|
let start = Date.now();
|
||
|
let loop = async() => {
|
||
|
try {
|
||
|
await fn();
|
||
|
} catch (err) {
|
||
|
if (Date.now() - start > timeout) {
|
||
|
throw err;
|
||
|
}
|
||
|
await new Promise((resolve) => setTimeout(resolve, interval))
|
||
|
await loop();
|
||
|
}
|
||
|
};
|
||
|
await loop();
|
||
|
};
|
||
|
module.exports = eventually;
|