UNPKG

web-crawling-utils

Version:
54 lines (53 loc) 2.55 kB
"use strict"; 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.withRetry = void 0; /** * Executes a given asynchronous function, retrying it upon failure up to a specified number of times. * This utility is particularly useful for handling transient errors in network requests or external API calls. * * @template T - The type of the resolved value from the asynchronous function. * @param {() => Promise<T>} fn - An asynchronous function to be executed, returning a Promise. * @param {number} [retries=3] - The maximum number of retry attempts. Defaults to 3. * @param {number} [delay=1000] - The wait time (in milliseconds) between retries. Defaults to 1000 ms. * * @returns {Promise<T>} - Returns a Promise that resolves with the result of `fn` if successful, or throws an error if all attempts fail. * * @throws {Error} - Throws an error if the function fails after the specified number of retries. * * @example * const fetchData = async () => { * // Simulate an API call * if (Math.random() > 0.5) throw new Error('Random failure'); * return 'Data received successfully'; * }; * * withRetry(fetchData, 5, 2000) * .then((data) => console.log(data)) * .catch((error) => console.error(error.message)); */ const withRetry = (fn_1, ...args_1) => __awaiter(void 0, [fn_1, ...args_1], void 0, function* (fn, retries = 3, delay = 1000) { let attempts = 0; while (attempts < retries) { try { return yield fn(); } catch (error) { attempts++; if (attempts >= retries) { throw new Error(`Failed after ${retries} attempts: ${error.message}`); } yield new Promise((resolve) => setTimeout(resolve, delay)); } } throw new Error('This point should not be reached'); }); exports.withRetry = withRetry;