diff options
author | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-01-10 20:50:26 +0800 |
---|---|---|
committer | Leonid Logvinov <logvinov.leon@gmail.com> | 2018-01-11 20:22:04 +0800 |
commit | 065570ebf57eb37b14ffd0b2fe131c3dcec4064a (patch) | |
tree | 439a56e7f10d6c581bebda262265a7aede2a7cd5 /packages/utils/src | |
parent | 1f5dfd71d514975cf7c26c693d0a4188bda5f12c (diff) | |
download | dexon-sol-tools-065570ebf57eb37b14ffd0b2fe131c3dcec4064a.tar dexon-sol-tools-065570ebf57eb37b14ffd0b2fe131c3dcec4064a.tar.gz dexon-sol-tools-065570ebf57eb37b14ffd0b2fe131c3dcec4064a.tar.bz2 dexon-sol-tools-065570ebf57eb37b14ffd0b2fe131c3dcec4064a.tar.lz dexon-sol-tools-065570ebf57eb37b14ffd0b2fe131c3dcec4064a.tar.xz dexon-sol-tools-065570ebf57eb37b14ffd0b2fe131c3dcec4064a.tar.zst dexon-sol-tools-065570ebf57eb37b14ffd0b2fe131c3dcec4064a.zip |
Add an error handler parameter to intervals
Diffstat (limited to 'packages/utils/src')
-rw-r--r-- | packages/utils/src/interval_utils.ts | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/packages/utils/src/interval_utils.ts b/packages/utils/src/interval_utils.ts index 62b79f2f5..ebecc7015 100644 --- a/packages/utils/src/interval_utils.ts +++ b/packages/utils/src/interval_utils.ts @@ -1,14 +1,18 @@ import * as _ from 'lodash'; export const intervalUtils = { - setAsyncExcludingInterval(fn: () => Promise<void>, intervalMs: number) { + setAsyncExcludingInterval(fn: () => Promise<void>, intervalMs: number, onError: (err: Error) => void) { let locked = false; const intervalId = setInterval(async () => { if (locked) { return; } else { locked = true; - await fn(); + try { + await fn(); + } catch (err) { + onError(err); + } locked = false; } }, intervalMs); @@ -17,4 +21,17 @@ export const intervalUtils = { clearAsyncExcludingInterval(intervalId: NodeJS.Timer): void { clearInterval(intervalId); }, + setInterval(fn: () => void, intervalMs: number, onError: (err: Error) => void) { + const intervalId = setInterval(() => { + try { + fn(); + } catch (err) { + onError(err); + } + }, intervalMs); + return intervalId; + }, + clearInterval(intervalId: NodeJS.Timer): void { + clearInterval(intervalId); + }, }; |