transf<R extends Object> method

SafeCompleter<R> transf<R extends Object>([
  1. @noFutures R noFutures(
    1. T e
    )?
])

Creates a new SafeCompleter by transforming the future value of this one.

When this completer finishes, its value will be passed to the noFutures function (or cast if null), and the result will be used to resolve the new completer.

Implementation

SafeCompleter<R> transf<R extends Object>([
  @noFutures R Function(T e)? noFutures,
]) {
  final newCompleter = SafeCompleter<R>();
  resolvable().then((e) {
    try {
      final result = noFutures != null ? noFutures(e) : (e as R);
      newCompleter.complete(result).end();
    } catch (error, stackTrace) {
      newCompleter
          .resolve(Sync.err(Err(error, stackTrace: stackTrace)))
          .end();
    }
    return e;
  }).end();
  return newCompleter;
}