mirror of
https://github.com/Kir-Antipov/mc-publish.git
synced 2025-01-24 10:39:09 -05:00
20 lines
745 B
TypeScript
20 lines
745 B
TypeScript
|
import sleep from "./sleep";
|
||
|
|
||
|
export async function retry<T>({ func, delay = 0, maxAttempts = -1, softErrorPredicate, errorCallback }: { func: () => T | Promise<T>, delay?: number, maxAttempts?: number, softErrorPredicate?: (error: unknown) => boolean, errorCallback?: (error: unknown) => void }): Promise<T> {
|
||
|
let attempts = 0;
|
||
|
while (true) {
|
||
|
try {
|
||
|
return await func();
|
||
|
} catch (e) {
|
||
|
const isSoft = softErrorPredicate ? softErrorPredicate(e) : e?.soft;
|
||
|
if (!isSoft || maxAttempts >= 0 && ++attempts >= maxAttempts ) {
|
||
|
throw e;
|
||
|
}
|
||
|
if (errorCallback) {
|
||
|
errorCallback(e);
|
||
|
}
|
||
|
}
|
||
|
await sleep(delay);
|
||
|
}
|
||
|
}
|