handleFutureWithMapper<T, S> function

JSPromise<JSAny?> handleFutureWithMapper<T, S>(
  1. Future<T> future,
  2. Func1<T, S> mapper
)

Handles the Future object with the provided mapper function.

Implementation

JSPromise handleFutureWithMapper<T, S>(Future<T> future, Func1<T, S> mapper) {
  return JSPromise(
    (JSFunction resolve, JSFunction reject) {
      future.then<void>(
        (T value) {
          final Object? target = mapper(value);
          final JSAny? jsVal = target?.jsify();
          resolve.callAsFunction(resolve, jsVal);
        },
        onError: (Object error, StackTrace stackTrace) {
          final errorConstructor =
              globalContext.getProperty('Error'.toJS)! as JSFunction;
          final wrapper = errorConstructor.callAsConstructor<JSObject>(
            'Dart exception: $error'.toJS,
          );
          wrapper['error'] = error.toJSBox;
          wrapper['stack'] = stackTrace.toString().toJS;
          reject.callAsFunction(reject, wrapper);
        },
      );
    }.toJS,
  );
}