syphonx-core
Version:
SyphonX is a template-driven solution for extracting data from HTML in a highly efficient way. It combines the power of jQuery, Regular Expressions, and Javascript into a declarative template-driven format that extracts and reshapes HTML data into JSON.
30 lines • 1.08 kB
JavaScript
import { sleep } from "./index.js";
export async function attempt(onTry, onBeforeRetry, options) {
const { retries = 2, retryDelay = [1, 10, 30], rethrow = true, quiet = false } = options || {};
if (!onBeforeRetry)
onBeforeRetry = () => true;
let retry = 0;
while (true) {
try {
const result = await onTry();
return { result };
}
catch (error) {
if (++retry <= retries && onBeforeRetry(error)) {
if (!quiet)
console.warn(`${error instanceof Error ? error.message : JSON.stringify(error)}`);
const seconds = retryDelay[retry - 1] || retryDelay[retryDelay.length - 1];
if (!quiet)
console.log(`RETRY #${retry}/${retries} sleeping for ${seconds} seconds...`);
await sleep(seconds * 1000);
}
else if (rethrow) {
throw error;
}
else {
return { error };
}
}
}
}
//# sourceMappingURL=attempt.js.map