first relase
This commit is contained in:
parent
d3a42ba6e9
commit
c6afce22ed
288 changed files with 55505 additions and 192 deletions
31
utils/index.ts
Normal file
31
utils/index.ts
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
export const sleep = (ms: number) => {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
};
|
||||
|
||||
export async function asyncRunSafe<T = any>(
|
||||
fn: Promise<T>,
|
||||
): Promise<[Error] | [null, T]> {
|
||||
try {
|
||||
return [null, await fn];
|
||||
} catch (e: any) {
|
||||
return [e || new Error('unknown error')];
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchWithRetry<T = any>(
|
||||
fn: Promise<T>,
|
||||
retries = 3,
|
||||
): Promise<[Error] | [null, T]> {
|
||||
const [error, res] = await asyncRunSafe(fn);
|
||||
if (error) {
|
||||
if (retries > 0) {
|
||||
const res = await fetchWithRetry(fn, retries - 1);
|
||||
return res;
|
||||
} else {
|
||||
if (error instanceof Error) return [error];
|
||||
return [new Error('unknown error')];
|
||||
}
|
||||
} else {
|
||||
return [null, res];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue