UNPKG

@hashgraph/hedera-local

Version:

Developer tooling for running Local Hedera Network (Consensus + Mirror Nodes).

58 lines 2.61 kB
"use strict"; // SPDX-License-Identifier: Apache-2.0 var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RetryUtils = void 0; class RetryUtils { /** * Retries a task up to a specified number of times. * * @param {() => Promise<T>} task - The task to retry. * @param {Object} options - The options for the retry. * @param [options.maxAttempts=3] - The maximum number of attempts. * @param [options.backOff=500] - The time to wait between attempts in milliseconds. * @param [options.doOnRetry=(error, attempt) => {}] - The function to call on each retry. * @returns {Promise<T>} - A promise that resolves with the result of the task. * @template T */ static retryTask(task_1) { return __awaiter(this, arguments, void 0, function* (task, { maxRetries = 3, backOff = 500, shouldRetry = (_error) => true, doOnRetry = (_error) => { }, } = {}) { let retries = 0; while (retries < maxRetries) { try { // eslint-disable-next-line no-await-in-loop return yield task(); } catch (error) { if (!shouldRetry(error) || retries === maxRetries - 1) { throw error; } doOnRetry(error); yield this.delay(backOff); } retries += 1; } /* istanbul ignore next */ throw new Error('Unreachable code'); }); } /** * Delays the execution of the task. * @param ms The time to wait in milliseconds. * @returns {Promise<void>} A promise that resolves after the delay. * @private */ static delay(ms) { return new Promise((resolve) => setTimeout(resolve, ms)); } } exports.RetryUtils = RetryUtils; //# sourceMappingURL=RetryUtils.js.map