@botonic/plugin-contentful
Version:
## What Does This Plugin Do?
37 lines • 1.2 kB
JavaScript
import { __awaiter } from "tslib";
export function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
export class ExponentialBackoff {
constructor(startMs = 10, times = 10) {
this.startMs = startMs;
this.times = times;
}
backoff() {
return __awaiter(this, void 0, void 0, function* () {
if (this.times <= 0) {
throw new Error('Aborting exponential backoff');
}
yield sleep(this.startMs);
this.startMs *= 2;
this.times++;
});
}
}
export function repeatWithBackoff(func, backoff = new ExponentialBackoff(), logger = console.log) {
return __awaiter(this, void 0, void 0, function* () {
for (;;) {
try {
return yield func();
}
catch (e) {
const stack = e.stack
? `\nat:\n${String(e.stack)}`
: ' (no stack available)';
logger(`Retrying after exception at ${new Date().toISOString()}: ${String(e)}` + stack);
yield backoff.backoff();
}
}
});
}
//# sourceMappingURL=backoff.js.map