mirror of
https://github.com/laurent22/joplin.git
synced 2024-12-09 08:45:55 +02:00
0edc66da49
Relates to #5389
30 lines
540 B
TypeScript
30 lines
540 B
TypeScript
/* eslint-disable import/prefer-default-export */
|
|
|
|
import { msleep } 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 msleep(opts.pause);
|
|
}
|
|
}
|
|
}
|
|
|
|
return null;
|
|
};
|