问题
It does not appear there is a clear interception of JS Promises by the Interop or dart2JS.
ServiceWorkerContainer swContain = window.navigator.serworker;
swContain.register(workerScriptURI,scope).then((ServiceWorkerRegistration rego){
/// Here confirm scope and the state, handle and unregister if required.
)};
Yet it appears to be no way of pulling this off without wrapping the promise and a completer. When I then I can not get to work in a dependable fashion.
Long post here: https://groups.google.com/a/dartlang.org/forum/#!topic/web/_DCR6vMBm7Y
WorkAround That I Am Running With
So this works well as of 0.6.0:
@JS("Promise")
class Promise {
external void then(Function onFulfilled, Function onRejected);
external Static Promise resolve (dynamic value);
}
@JS("ServiceWorkerContainer")
class ServiceWorkerContainer {
external Promise register(String scriptURL, Map options)
}
main(){
ServiceWorkContainer swCTX = navigator.serviceWorker;
successHandler(dynamic value) {
// Call Promise resolve(value) if the not settled. Ultimately
ServiceWorkerRegistration swRego = value;
// Now call anything you want on the Rego, just wrap what you need.
failureHandler(dynamic value) {
// Deal with it here
/// This the main show here.
swCTX.register('script-name.dart.js', scope).then(allowInterop(successHandler), allowInterop(failureHandler))
}
I tried blending the above with the existing 'dart:html' library, in an attempt not to wrap so much. But it got really messy with hiding imports and all sort of stuff.
All the dancing around will become pointless as soon as async/await get lined up with promises. I have omitted some of the wrapping, basicly if you need the Object or a value from it you need to wrap.
来源:https://stackoverflow.com/questions/34572517/dart-js-interop-0-6-0-and-js-promises-resolving