asyncTry<T> function

Future<T?> asyncTry<T>({
  1. required Future<T?> action(),
  2. FOnError? onError,
  3. FSetError? setError,
  4. T? defaultValue,
})

Implementation

Future<T?> asyncTry<T>({required Future<T?> Function() action, FOnError? onError, FSetError? setError, T? defaultValue}) async {
  try {
    return await action();
  } catch (ex, stackTrace) {
    await onError?.call(ex, stackTrace);
    if (setError != null) {
      final err = setError.call();
      err.isError = true;
      err.err = ex;
      err.stackTrace = stackTrace;
    }
  }

  return defaultValue;
}