about summary refs log tree commit diff
path: root/src/lib/async/until.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/async/until.ts')
-rw-r--r--src/lib/async/until.ts24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/lib/async/until.ts b/src/lib/async/until.ts
new file mode 100644
index 000000000..db53c9218
--- /dev/null
+++ b/src/lib/async/until.ts
@@ -0,0 +1,24 @@
+import {timeout} from './timeout'
+
+export async function until(
+  retries: number,
+  delay: number,
+  cond: (v: any, err: any) => boolean,
+  fn: () => Promise<any>,
+): Promise<boolean> {
+  while (retries > 0) {
+    try {
+      const v = await fn()
+      if (cond(v, undefined)) {
+        return true
+      }
+    } catch (e: any) {
+      if (cond(undefined, e)) {
+        return true
+      }
+    }
+    await timeout(delay)
+    retries--
+  }
+  return false
+}