1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-11-24 08:12:24 +02:00
joplin/packages/utils/net.ts
2023-04-04 12:04:42 +02:00

30 lines
538 B
TypeScript

/* eslint-disable import/prefer-default-export */
import { sleep } from './time';
import fetch from 'node-fetch';
export const fetchWithRetry = async (url: string, opts: any = null) => {
if (!opts) opts = {};
let retry = opts && opts.retry || 3;
while (retry > 0) {
try {
return fetch(url, opts);
} catch (e) {
if (opts && opts.callback) {
opts.callback(retry);
}
retry = retry - 1;
if (retry === 0) {
throw e;
}
if (opts && opts.pause) {
await sleep(opts.pause);
}
}
}
return null;
};