You've already forked joplin
mirror of
https://github.com/laurent22/joplin.git
synced 2025-11-23 22:36:32 +02:00
22 lines
427 B
TypeScript
22 lines
427 B
TypeScript
interface Options {
|
|
count: number;
|
|
onFail: (error: Error)=> Promise<void>;
|
|
}
|
|
|
|
const retryWithCount = async (task: ()=> Promise<void>, { count, onFail }: Options) => {
|
|
let lastError: Error|null = null;
|
|
for (let retry = 0; retry < count; retry ++) {
|
|
try {
|
|
return await task();
|
|
} catch (error) {
|
|
await onFail(error);
|
|
lastError = error;
|
|
}
|
|
}
|
|
|
|
if (lastError) throw lastError;
|
|
};
|
|
|
|
export default retryWithCount;
|
|
|