parent
1ae9b108fd
commit
88a87504d1
2 changed files with 37 additions and 20 deletions
@ -0,0 +1,25 @@ |
||||
const assert = require('assert'); |
||||
|
||||
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; |
Reference in new issue