Configurable polling workflows with backoff, jitter, abort support, and response-time metrics.
Import
import { PollingHelper, createPoller } from "bytekit/polling-helper";
What it does
PollingHelper repeatedly calls an async function on a schedule until a stop condition is met, a limit is reached, or the caller aborts. It supports exponential and linear backoff, jitter, per-attempt timeouts, and lifecycle hooks.
Constructor
const poller = new PollingHelper<T>(fn: () => Promise<T>, options?: PollingOptions<T>);
PollingOptions<T>
Property
Type
Default
Description
interval
number
1000
Base interval between attempts (ms).
maxAttempts
number
Infinity
Maximum number of attempts.
maxDuration
number
Infinity
Maximum total polling duration (ms).
backoffMultiplier
number
1
Multiplier applied to interval after each attempt.
maxBackoffInterval
number
30000
Upper bound for the computed interval.
exponentialBase
number
2
Base for exponential backoff calculation.
jitter
false | number
false
Add random jitter. false disables it; a number 0–100 sets the jitter percentage.
attemptTimeout
number
—
Timeout per individual attempt (ms).
retryOnError
boolean
true
Continue polling when an attempt throws.
stopCondition
(result: T) => boolean
—
Return true to stop polling early.
onAttempt
(attempt: number) => void
—
Called before each attempt.
onSuccess
(result: T) => void
—
Called when an attempt succeeds.
onError
(error: Error) => void
—
Called when an attempt fails.
Instance methods
Method
Returns
Description
start()
Promise<PollingResult<T>>
Run the polling loop until completion.
startWithAbort()
Promise<PollingResult<T>>
Same as start(), but can be cancelled via abort().
abort()
void
Cancel a running poll started with startWithAbort().
PollingResult<T>
Property
Type
Description
success
boolean
Whether polling ended with a fulfilled stop condition.
result
T | undefined
Last successful result.
error
Error | undefined
Last error, if any.
attempts
number
Total attempts executed.
duration
number
Total elapsed time (ms).
metrics
object | undefined
Response-time statistics (see below).
metrics
Property
Type
minResponseTime
number
maxResponseTime
number
avgResponseTime
number
Static methods
Method
Description
PollingHelper.poll(fn, options?)
One-shot poll using default options.
PollingHelper.pollWithBackoff(fn, options?)
Poll with exponential backoff pre-configured.
PollingHelper.pollWithLinearBackoff(fn, options?)
Poll with linear backoff pre-configured.
Factory
const poller = createPoller(fn, options?);
Returns a PollingHelper instance — useful when you want to configure once and call start() later.