UNPKG

@getclave/lifi-sdk

Version:

LI.FI Any-to-Any Cross-Chain-Swap SDK

38 lines (37 loc) 1.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.waitForResult = void 0; const sleep_1 = require("./sleep"); /** * Repeatedly calls a given asynchronous function until it resolves with a value * @param fn The function that should be repeated * @param interval The timeout in milliseconds between retries, defaults to 5000 * @param maxRetries Maximum number of retries before throwing an error, defaults to 3 * @param shouldRetry Optional predicate to determine if an error should trigger a retry * @returns The result of the fn function * @throws Error if maximum retries is reached, if function keeps returning undefined, or if shouldRetry returns false */ const waitForResult = async (fn, interval = 5000, maxRetries = 3, shouldRetry = () => true) => { let result; let attempts = 0; while (!result) { try { result = await fn(); if (!result) { await (0, sleep_1.sleep)(interval); } } catch (error) { if (!shouldRetry(attempts, error)) { throw error; } attempts++; if (attempts === maxRetries) { throw error; } await (0, sleep_1.sleep)(interval); } } return result; }; exports.waitForResult = waitForResult;