Skip to content

Commit

Permalink
fix(Promise): Make sure we check for native Promise before es6-promis…
Browse files Browse the repository at this point in the history
…e gets a chance to polyfill
  • Loading branch information
vicb committed Dec 11, 2015
1 parent 364db43 commit fa18d4c
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions lib/microtask.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
'use strict';

// TODO(vicb): Create a benchmark for the different methods & the usage of the queue
// see https://github.com/angular/zone.js/issues/97

// It is required to initialize hasNativePromise before requiring es6-promise otherwise es6-promise would
// overwrite the native Promise implementation on v8 and the check would always return false.
// see https://github.com/jakearchibald/es6-promise/issues/140
var hasNativePromise = typeof Promise !== "undefined" &&
Promise.toString().indexOf("[native code]") !== -1;

var isFirefox = global.navigator &&
global.navigator.userAgent.toLowerCase().indexOf('firefox') > -1;

var resolvedPromise;

// TODO(vicb): remove '!isFirefox' when the bug gets fixed:
// https://bugzilla.mozilla.org/show_bug.cgi?id=1162013
if (hasNativePromise && !isFirefox) {
// When available use a native Promise to schedule microtasks.
// When not available, es6-promise fallback will be used
resolvedPromise = Promise.resolve();
}

var es6Promise = require('es6-promise').Promise;

if (resolvedPromise) {
es6Promise._setScheduler(function(fn) {
resolvedPromise.then(fn);
});
}

// es6-promise asap should schedule microtasks via zone.scheduleMicrotask so that any
// user defined hooks are triggered
es6Promise._setAsap(function(fn, arg) {
Expand All @@ -25,23 +53,5 @@ module.exports = {
addMicrotaskSupport: addMicrotaskSupport
};

// TODO(vicb): Create a benchmark for the different methods & the usage of the queue
// see https://github.com/angular/zone.js/issues/97

var hasNativePromise = typeof Promise !== "undefined" &&
Promise.toString().indexOf("[native code]") !== -1;

var isFirefox = global.navigator &&
global.navigator.userAgent.toLowerCase().indexOf('firefox') > -1;

// TODO(vicb): remove '!isFirefox' when the bug gets fixed:
// https://bugzilla.mozilla.org/show_bug.cgi?id=1162013
if (hasNativePromise && !isFirefox) {
// When available use a native Promise to schedule microtasks.
// When not available, es6-promise fallback will be used
var resolvedPromise = Promise.resolve();
es6Promise._setScheduler(function(fn) {
resolvedPromise.then(fn);
});
}

0 comments on commit fa18d4c

Please sign in to comment.