_From @jmesserly on June 25, 2015 20:50_
For JS interop, it would be ideal if we could adapt Dart Futures to implement the Promise interface.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
This will also ease DOM interop as Promise becomes more common.
Ideally, Dart Future actually is a JS Promise, simply exposing different user-facing APIs (similar to Dart builtin List type and JS Array). But we could also try adapters, similar to Dart Iterable objects exposing a [Symbol.iterator]() method to JS, and Dart for-each consuming JS Iterables.
_Copied from original issue: dart-lang/dev_compiler#245_
related #221
_From @jacob314 on January 4, 2016 19:10_
This is now becoming an issue for JS interop
yeah, just wanted to add an update: this is something we're definitely doing, only question is when it happens. Probably after we've gotten the Dart language tests and modular compilation in a good place.
and SDK issue is https://github.com/dart-lang/sdk/issues/24679
This issue has been hanging around for almost a year.
Actually I first hit it here. From my understanding there has not been any work on this. I know that @alan-knight was playing some Service worker stuff (using all dart:html), but look at the repo it looks like it did not move forward, possibly because the register required handling the promise.
If my assumptions above are correct?
Then certainly this needs to addressed in a once and for all, promises are spreading in JS and have it map one to one with a Future in the dart2JS is reasonable. Otherwise there will be just be promise completer boilerplate all over the place.
There is a type definition for es6 promises yet JS Facade Gen does not appear to handle it too well @jacob314.
Hence I for one would like to this issue given some priority and clarity.
Hit this via https://github.com/dart-lang/sdk/issues/28422.
I'm going to see if we can do adapters in both directions:
Tricky part here is figuring out how to inject "then" and "catch" because those aren't symbolized and collide with the Dart names. Then again ... once the Future interface is marked as "may be native" we might get the renaming for free.
EDIT: ah, because JavaScript Promise recognizes the mere existence of "then", we may need to use "dartx.then" on all Dart objects, kinda similar to "toString", so it doesn't pick up accidentally and be treated as a Promise "then".
Would be nice to have this.
If/when dart compilers get native support of JS Promises, please don't tie it to dart:html.
FWIW, we're using the following boilerplate which works mostly fine both in browsers and node:
@JS()
class Promise<T> {
external Promise(void executor(void resolve(T result), Function reject));
external Promise then(void onFulfilled(T result), [Function onRejected]);
}
To create a Promise from Future (to send it to JS world):
return new Promise<MyType>(allowInterop((resolve, reject) {
myFuture.then(resolve, onError: reject);
}));
To create a Future from Promise (gotten from JS world):
final completer = new Completer<MyType>();
myPromise.then(allowInterop(completer.complete),
allowInterop(completer.completeError));
return completer.future;
If/when dart compilers get native support of JS Promises, please don't tie it to
dart:html.FWIW, we're using the following boilerplate which works mostly fine both in browsers and node:
@JS() class Promise<T> { external Promise(void executor(void resolve(T result), Function reject)); external Promise then(void onFulfilled(T result), [Function onRejected]); }To create a
PromisefromFuture(to send it to JS world):return new Promise<MyType>(allowInterop((resolve, reject) { myFuture.then(resolve, onError: reject); }));To create a
FuturefromPromise(gotten from JS world):final completer = new Completer<MyType>(); myPromise.then(allowInterop(completer.complete), allowInterop(completer.completeError)); return completer.future;
https://github.com/flutter/flutter/issues/46973
Flutter for Web is beta now. The Promise implementation seems to work in debug mode but not profile/release mode.
@lexaknyazev Could you show some light on me?
Most helpful comment
If/when dart compilers get native support of JS Promises, please don't tie it to
dart:html.FWIW, we're using the following boilerplate which works mostly fine both in browsers and node:
To create a
PromisefromFuture(to send it to JS world):To create a
FuturefromPromise(gotten from JS world):