asyncTry<R> function

FutureOr<R?> asyncTry<R>(
  1. FutureOr<R?> tryBlock(), {
  2. FutureOr<R?> then(
    1. R? r
    )?,
  3. Function? onError,
  4. FutureOr<void> onFinally()?,
})

Executes a tryBlock in a try, then, catch and finally execution chain.

  • then is called after execution of tryBlock.
  • onError is called if tryBlock or then throws some error. Similar to Future.then onError parameter.
  • onFinally is called after execution of tryBlock, then and onError.

Implementation

FutureOr<R?> asyncTry<R>(FutureOr<R?> Function() tryBlock,
    {FutureOr<R?> Function(R? r)? then,
    Function? onError,
    FutureOr<void> Function()? onFinally}) {
  var thenFunction = then != null ? _ThenFunction<R>(then) : null;
  var errorFunction = onError != null ? _ErrorFunction<R>(onError) : null;
  var finallyFunction =
      onFinally != null ? _FinallyFunction<R>(onFinally) : null;

  try {
    var ret = tryBlock();

    if (ret is Future<R?>) {
      return _returnFuture<R>(
          ret, thenFunction, errorFunction, finallyFunction);
    } else {
      return _returnValue<R>(ret, thenFunction, errorFunction, finallyFunction);
    }
  } catch (e, s) {
    return _completeError(e, s, errorFunction, finallyFunction);
  }
}