diff options
author | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-09-22 16:13:12 +0900 |
---|---|---|
committer | Shin'ya Ueoka <ueokande@i-beam.org> | 2019-09-22 16:13:12 +0900 |
commit | 7e77e31ad64c3649888eae337e0c984fd9c2ea2a (patch) | |
tree | 4d3e630f82712abce5afd7e498d514253464a6ea /e2e/eventually.ts | |
parent | 91f8383ecc2f7ef3f95173ad973a74f79e277a6c (diff) |
Migrate e2e tests to TypeScript
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; |