UNPKG

@daiso-tech/core

Version:

The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.

29 lines 1.03 kB
/** * @module Async */ import { TimeSpan } from "../../../utilities/_module-exports.js"; import { withJitter } from "../../../async/backof-policies/_shared.js"; /** * Polynomial backoff policy with jitter * * IMPORT_PATH: `"@daiso-tech/core/async"` * @group BackoffPolicies */ export function polynomialBackoffPolicy(settings = {}) { return (attempt, error) => { if (typeof settings === "function") { settings = settings(error); } let { maxDelay = 6000, minDelay = 1_000 } = settings; if (maxDelay instanceof TimeSpan) { maxDelay = maxDelay.toMilliseconds(); } if (minDelay instanceof TimeSpan) { minDelay = minDelay.toMilliseconds(); } const { degree = 2, jitter = 0.5, _mathRandom = Math.random, } = settings; const polynomial = Math.min(maxDelay, minDelay * Math.pow(attempt, degree)); return withJitter(jitter, polynomial, _mathRandom); }; } //# sourceMappingURL=polynomial-backoff-policy.js.map