@crowdin/crowdin-api-client
Version:
JavaScript library for Crowdin API
60 lines (59 loc) • 1.78 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.RetryService = void 0;
/**
* @internal
*/
class RetryService {
constructor(config) {
this.config = config;
}
/**
* @param func function to execute
*/
async executeAsyncFunc(func) {
for (let i = 0; i <= this.config.retries; i++) {
try {
const result = await func();
return result;
}
catch (error) {
const skip = this.config.conditions
.map((condition) => condition.test(error))
.find((skip) => skip === true);
if (skip || i === this.config.retries) {
throw error;
}
await this.wait();
}
}
throw new Error('Wrong retry configuration. Failed to retrieve value.');
}
/**
* @param func function to execute
*/
async executeSyncFunc(func) {
for (let i = 0; i <= this.config.retries; i++) {
try {
const result = func();
return result;
}
catch (error) {
const skip = this.config.conditions
.map((condition) => condition.test(error))
.find((skip) => skip === true);
if (skip || i === this.config.retries) {
throw error;
}
await this.wait();
}
}
throw new Error('Wrong retry configuration. Failed to retrieve value.');
}
wait() {
return new Promise((res) => {
setTimeout(() => res(), this.config.waitInterval);
});
}
}
exports.RetryService = RetryService;
;