forked from
jollywhoppers.com/witchsky.app
Bluesky app fork with some witchin' additions 馃挮
1import {timeout} from '#/lib/async/timeout'
2import {isNetworkError} from '#/lib/strings/errors'
3
4export async function retry<P>(
5 retries: number,
6 shouldRetry: (err: any) => boolean,
7 action: () => Promise<P>,
8 delay?: number,
9): Promise<P> {
10 let lastErr
11 while (retries > 0) {
12 try {
13 return await action()
14 } catch (e: any) {
15 lastErr = e
16 if (shouldRetry(e)) {
17 if (delay) {
18 await timeout(delay)
19 }
20 retries--
21 continue
22 }
23 throw e
24 }
25 }
26 throw lastErr
27}
28
29export async function networkRetry<P>(
30 retries: number,
31 fn: () => Promise<P>,
32): Promise<P> {
33 return retry(retries, isNetworkError, fn)
34}