mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import {timeout} from './timeout'
2
3export async function until<T>(
4 retries: number,
5 delay: number,
6 cond: (v: T, err: any) => boolean,
7 fn: () => Promise<T>,
8): Promise<boolean> {
9 while (retries > 0) {
10 try {
11 const v = await fn()
12 if (cond(v, undefined)) {
13 return true
14 }
15 } catch (e: any) {
16 // TODO: change the type signature of cond to accept undefined
17 // however this breaks every existing usage of until -sfn
18 if (cond(undefined as unknown as T, e)) {
19 return true
20 }
21 }
22 await timeout(delay)
23 retries--
24 }
25 return false
26}