addWait<T> method

void addWait<T>(
  1. Future<T> future, {
  2. required dynamic onSuccess(
    1. T result
    ),
  3. dynamic onError(
    1. dynamic e,
    2. dynamic s
    )?,
})

Add operation to list and remove when is finish

Implementation

void addWait<T>(
  Future<T> future, {
  required Function(T result) onSuccess,
  Function(dynamic e, dynamic s)? onError,
}) {
  CancelableOperation<T> operation = CancelableOperation.fromFuture(future);

  add(operation);
  int index = indexOf(operation);

  this[index].value
      .then((result) {
        onSuccess(result);
        removeAt(index);
      })
      .catchError((e, s) {
        onError?.call(e, s);
        removeAt(index);
      });
}