angular dart js interop with async / promise awaited in client angular dart js interop with async / promise awaited in client dart dart

angular dart js interop with async / promise awaited in client


You can easily convert a Javascript Promise to a Dart Future, by using the convertNativePromiseToDartFuture API, which is available in dart:html_common.

A simple implementation might look like :

Javascript :

function myCoolFunc () {  return new Promise((resolve, reject) => {    resolve(myLongAwaitedData);  });}

Dart Interop file :

@JS()library coolLib;import 'package:js/js.dart';import 'dart:async';@JS()external Future<T> myCoolFunc ();

Dart file :

import 'dart:html_common';import 'cool_lib.dart';main() async {  var myVar = await convertNativePromiseToDartFuture(myCoolFunc());  print(myVar);}

I found this deeply buried in the Gitter of the Dart Sdk. I hope it can help future Angular Dart users.

Update : This API has changed in Dart 2.6 convertNativePromiseToDartFuture has been replaced with promiseToFuture