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.

example/poll_until.dart

// Runnable tour of pollUntil:
//   dart example/poll_until.dart
//
// Shows: polling an async source until a condition holds, with exponential
// backoff and a deadline.
import 'package:poll_until/poll_until.dart';

Future<void> main() async {
  // A flaky "job status" endpoint: ready after ~3 checks.
  var checks = 0;
  Future<String> jobStatus() async {
    checks++;
    return checks >= 3 ? 'done' : 'running';
  }

  final result = await pollUntil(
    jobStatus,
    isDone: (status) => status == 'done',
    initialDelay: const Duration(milliseconds: 10),
    backoff: ExponentialBackoff(
      initial: const Duration(milliseconds: 10),
      factor: 2,
      max: const Duration(milliseconds: 200),
    ),
    timeout: const Duration(seconds: 5),
  );

  print('status=${result.value} after ${result.attempts} attempt(s), '
      '${result.elapsed.inMilliseconds}ms total');
}
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