UNPKG

@reservoir0x/relay-sdk

Version:

Relay is the Fastest and Cheapest Way to Bridge and Transact Across Chains.

40 lines 1.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.pollUntilOk = exports.pollUntilHasData = void 0; const axios_js_1 = require("./axios.js"); async function pollUntilHasData(request, dataParser, maximumAttempts = 15, attemptCount = 0) { if (attemptCount >= maximumAttempts) { throw `Failed to get data after ${attemptCount} attempt(s), aborting`; } async function getData() { let res = await axios_js_1.axios.request(request); return res.data; } const json = await getData(); const dataExists = dataParser(json); if (dataExists) return json; await new Promise((resolve) => setTimeout(resolve, 5000)); attemptCount++; await pollUntilHasData(request, dataParser, maximumAttempts, attemptCount); } exports.pollUntilHasData = pollUntilHasData; async function pollUntilOk(request, validate, maximumAttempts = 15, attemptCount = 0, pollingInterval = 5000) { if (attemptCount >= maximumAttempts) { throw `Failed to get an ok response after ${attemptCount} attempt(s), aborting`; } const res = await axios_js_1.axios.request(request); if (!validate) { validate = (res) => res.status === 200; } if (validate(res)) { return true; } else { await new Promise((resolve) => setTimeout(resolve, pollingInterval)); attemptCount++; await pollUntilOk(request, validate, maximumAttempts, attemptCount, pollingInterval); } } exports.pollUntilOk = pollUntilOk; //# sourceMappingURL=pollApi.js.map