my fork of the bluesky client
1import {isNetworkError} from '#/lib/strings/errors'
2
3export async function retry<P>(
4 retries: number,
5 cond: (err: any) => boolean,
6 fn: () => Promise<P>,
7): Promise<P> {
8 let lastErr
9 while (retries > 0) {
10 try {
11 return await fn()
12 } catch (e: any) {
13 lastErr = e
14 if (cond(e)) {
15 retries--
16 continue
17 }
18 throw e
19 }
20 }
21 throw lastErr
22}
23
24export async function networkRetry<P>(
25 retries: number,
26 fn: () => Promise<P>,
27): Promise<P> {
28 return retry(retries, isNetworkError, fn)
29}