Future doWhile(dynamic f())

Performs an async operation repeatedly until it returns false.

The function f is called repeatedly while it returns either the bool value true or a Future which completes with the value true.

If a call to f returns false or a Future that completes to false, iteration ends and the future returned by doWhile is completed.

If a future returned by f completes with an error, iteration ends and the future returned by doWhile completes with the same error.

The f function must return either a bool value or a Future completing with a bool value.

Source

static Future doWhile(f()) {
  _Future doneSignal = new _Future();
  var nextIteration;
  // Bind this callback explicitly so that each iteration isn't bound in the
  // context of all the previous iterations' callbacks.
  nextIteration = Zone.current.bindUnaryCallback((bool keepGoing) {
    if (keepGoing) {
      new Future.sync(f).then(nextIteration,
                              onError: doneSignal._completeError);
    } else {
      doneSignal._complete(null);
    }
  }, runGuarded: true);
  nextIteration(true);
  return doneSignal;
}