toJS 属性

JSPromise<JSAny?> toJS

一个 JSPromise,要么在当前 Future 完成后解决,要么以包含其错误的对象拒绝。

拒绝的对象包含原始错误作为一个 JSBoxedDartObject 在属性 error 中,以及原始堆栈跟踪作为一个 String 在属性 stack 中。

实现

JSPromise get toJS {
  return JSPromise((JSFunction resolve, JSFunction reject) {
    this.then((_) => resolve.callAsFunction(resolve),
        onError: (Object error, StackTrace stackTrace) {
      // TODO(srujzs): Can we do something better here? This is pretty much
      // useless to the user unless they call a Dart callback that consumes
      // this value and unboxes.
      final errorConstructor = globalContext['Error'] 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);
    });
  }.toJS);
}