I'm including Zone 0.6.21 in a project that also uses Bluebird as one of the first steps for using Angular 2. Unfortunately, there does not seem to be a clean way for having the two libraries work together.
Bluebird is more than a "Promise shim", since it replaces the Promise implementation entirely, with a more performant one, while also adding many utilities both to the Promise constructor and its prototype.
- If Bluebird is included before Zone (which I understand is the preferred way), Zone will overwrite the entire
Promise constructor, loosing all utilities and the internal implementation of Bluebird. This basically defeats the purpose of including Bluebird in the first place.
- If Bluebird is included after Zone and is made Zone-aware by replacing its scheduling function then everything works: Bluebird is active and zones work correctly. However the
Zone.assertZonePatched that Angular 2 calls will fail, since the Promise constructor is no longer the one from Zone.
Curently I can work around the problem by including Bluebird after Zone and then manually patching each function in the Bluebird API.
Promise.prototype.then = (function(_orig) {
return function(fulfilledHandler, rejectedHandler) {
return _orig.call(this, zonify(fulfilledHandler), zonify(rejectedHandler));
}
})(Promise.prototype.then);
// similar patches for other 25+ functions
// hack to keep Angular happy
Zone.assertZonePatched = function() {};
If you are interested, this Gist shows the full code.
As you can see it is pretty hacky and I would better go without it if it was possible.
Is there a particular reason for Zone introducing an entire Promise constructor instead of just wrapping then, catch and other methods?
I'm including Zone 0.6.21 in a project that also uses Bluebird as one of the first steps for using Angular 2. Unfortunately, there does not seem to be a clean way for having the two libraries work together.
Bluebird is more than a "Promise shim", since it replaces the
Promiseimplementation entirely, with a more performant one, while also adding many utilities both to thePromiseconstructor and its prototype.Promiseconstructor, loosing all utilities and the internal implementation of Bluebird. This basically defeats the purpose of including Bluebird in the first place.Zone.assertZonePatchedthat Angular 2 calls will fail, since thePromiseconstructor is no longer the one from Zone.Curently I can work around the problem by including Bluebird after Zone and then manually patching each function in the Bluebird API.
If you are interested, this Gist shows the full code.
As you can see it is pretty hacky and I would better go without it if it was possible.
Is there a particular reason for Zone introducing an entire
Promiseconstructor instead of just wrappingthen,catchand other methods?