handleFutureWithMapper<T, S> function

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

Handles the Future object with the provided mapper function.

Implementation

JSPromise handleFutureWithMapper<T, S>(
  Future<JSAny?> future,
  Func1<T, S> mapper,
) {
  // Taken from js_interop:286
  return JSPromise((JSFunction resolve, JSFunction reject) {
    future.then((JSAny? value) {
      resolve.callAsFunction(resolve, value);
      return value;
    }, onError: (Object error, StackTrace stackTrace) {
      final errorConstructor =
          globalContext.getProperty('Error'.toJS)! as JSFunction;
      final wrapper = errorConstructor.callAsConstructor<JSObject>(
          'Dart exception thrown from converted Future. Use the properties '
                  "'error' to fetch the boxed error and 'stack' to recover "
                  'the stack trace.'
              .toJS);
      wrapper['error'] = error.toJSBox;
      wrapper['stack'] = stackTrace.toString().toJS;
      reject.callAsFunction(reject, wrapper);
      return wrapper;
    });
  }.toJS);
}