bsky-app/src/lib/async/retry.ts
Paul Frazee 3c05a08482 Logout bug hunt (#294)
* Stop storing the log on disk

* Add more info to the session logging

* Only clear session tokens from storage when they've expired

* Retry session resumption a few times if it's a network issue

* Improvements to the 'connecting' screen
2023-03-15 15:05:13 -05:00

29 lines
537 B
TypeScript

import {isNetworkError} from 'lib/strings/errors'
export async function retry<P>(
retries: number,
cond: (err: any) => boolean,
fn: () => Promise<P>,
): Promise<P> {
let lastErr
while (retries > 0) {
try {
return await fn()
} catch (e: any) {
lastErr = e
if (cond(e)) {
retries--
continue
}
throw e
}
}
throw lastErr
}
export async function networkRetry<P>(
retries: number,
fn: () => Promise<P>,
): Promise<P> {
return retry(retries, isNetworkError, fn)
}