asyncTry<R> function
Executes a tryBlock
in a try
, then
, catch
and finally
execution chain.
then
is called after execution oftryBlock
.onError
is called iftryBlock
orthen
throws some error. Similar to Future.thenonError
parameter.onFinally
is called after execution oftryBlock
,then
andonError
.
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);
}
}