hexo/node_modules/ali-oss/lib/common/utils/retry.js

31 lines
941 B
JavaScript
Raw Normal View History

2023-10-03 11:14:36 +08:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.retry = void 0;
function retry(func, retryMax, config = {}) {
let retryNum = 0;
const { retryDelay = 500, errorHandler = () => true } = config;
const funcR = (...arg) => {
return new Promise((resolve, reject) => {
func(...arg)
.then(result => {
retryNum = 0;
resolve(result);
})
.catch(err => {
if (retryNum < retryMax && errorHandler(err)) {
retryNum++;
setTimeout(() => {
resolve(funcR(...arg));
}, retryDelay);
}
else {
retryNum = 0;
reject(err);
}
});
});
};
return funcR;
}
exports.retry = retry;