diff options
Diffstat (limited to 'e2e/eventually.ts')
-rw-r--r-- | e2e/eventually.ts | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/e2e/eventually.ts b/e2e/eventually.ts new file mode 100644 index 0000000..12c4552 --- /dev/null +++ b/e2e/eventually.ts @@ -0,0 +1,30 @@ +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; |