promiseToFuture<T> function

Future<T> promiseToFuture <T>(dynamic jsPromise)

Converts a JavaScript Promise to a Dart Future.

@JS()
external Promise<num> get threePromise; // Resolves to 3

final Future<num> threeFuture = promiseToFuture(threePromise);

final three = await threeFuture; // == 3

Implementation

Future<T> promiseToFuture<T>(jsPromise) {
  final completer = Completer<T>();

  final success = convertDartClosureToJS((r) => completer.complete(r), 1);
  final error = convertDartClosureToJS((e) => completer.completeError(e), 1);

  JS('', '#.then(#, #)', jsPromise, success, error);
  return completer.future;
}