wait method

Future<void> wait({
  1. CancellationToken? token,
})

If the token is in the paused state, then it pauses execution of the calling code until it receives the resume signal.
If the token is not in the paused state, then execution of the calling code continues immediately.

If a cancellation token is specified, the method may throw an TaskCanceledError exception.

Implementation

Future<void> wait({CancellationToken? token}) {
  if (token == null) {
    return _event.wait();
  }

  token.throwIfCancelled();
  final completer = Completer<void>();
  () async {
    await _event.wait();
    completer.tryComplete();
  }();
  return token.runCancellable(completer.tryComplete, () => completer.future);
}