Skip to main content

poll_until 0.1.0 copy "poll_until: ^0.1.0" to clipboard
poll_until: ^0.1.0 copied to clipboard

Poll an async function until a condition is met, with fixed or exponential backoff, deadline, and cancellation.

poll_until #

Poll an async function until a condition is met — Dart & Flutter.

Fixed or exponential backoff between attempts, a deadline, cooperative cancellation, and deterministic tests via injectable sleep/clock.

CI License: MIT

Why #

Waiting for eventual consistency is everywhere: deployment status, job completion, OTP email arrival, an ML inference result. The ad-hoc versions (while (!done) { await Future.delayed(...); }) miss the same three things: a deadline, growing backoff so you don't hammer the endpoint, and cancellation when the user navigates away. And they're miserable to test because real time is baked in.

final deployment = await pollUntil(
  () => api.deploymentStatus(id),
  isDone: (status) => status.state != 'pending',
  backoff: PollBackoff.exponential(initial: Duration(seconds: 1), factor: 2),
  timeout: Duration(minutes: 5),
);

Cancellable variant for screens the user can leave:

final token = PollCancellationToken();
subscription = routeObserver.onLeave.listen((_) => token.cancel());

final result = await pollUntilCancellable(
  () => api.invoiceState(id),
  isDone: (state) => state.isFinal,
  token: token,
  backoff: PollBackoff.fixed(Duration(seconds: 2)),
);

Time is injected (sleep, clock), so tests run instantly and assert the exact delays used — see the suite: 9 tests covering first-hit success, deadline diagnostics, immediate cancellation, fixed and capped-exponential backoff schedules, initial delay, and elapsed reporting.

dart test

License #

MIT

0
likes
160
points
65
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Poll an async function until a condition is met, with fixed or exponential backoff, deadline, and cancellation.

Repository (GitHub)
View/report issues

Topics

#async #polling #retry #dart #flutter

License

MIT (license)

More

Packages that depend on poll_until