Skip to content

Function: poll()

poll<T, A>(producer, options): AsyncGenerator<T, void, void>

Async iterable that drives a periodic poll. Each iteration:

  1. Builds a PollContext (attempt, previous, signal, shared attributes) and calls producer(ctx); yields the resolved value (subject to filter).
  2. Evaluates options.predicate(value, ctx); stops when it returns (or resolves to) false.
  3. Sleeps options.intervalMs before the next attempt.

The first call runs immediately (no leading sleep) so the consumer sees a value without waiting an interval. Errors thrown by producer propagate through the generator.

poll always creates an internal AbortController and exposes internal.signal as ctx.signal, so producers can rely on a defined signal without a nullish check. The external options.signal is tied in, and a try/finally aborts the internal signal when the consumer breaks out of the for await (or the loop throws), so any producer work still holding the signal sees the cancellation too.

T

A = Record<string, unknown>

PollProducer<T, A>

PollOptions<T, A>

AsyncGenerator<T, void, void>

for await (const msg of poll(
async ({ signal }) =>
client.genie.getMessage({ ... }, { abortSignal: signal }),
{
intervalMs: 250,
predicate: (m) => !TERMINAL_STATUSES.has(m.status),
signal: controller.signal,
},
)) {
render(msg);
}