Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit fa18d4c

Browse files
committed
fix(Promise): Make sure we check for native Promise before es6-promise gets a chance to polyfill
1 parent 364db43 commit fa18d4c

File tree

1 file changed

+28
-18
lines changed

1 file changed

+28
-18
lines changed

lib/microtask.js

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,35 @@
11
'use strict';
22

3+
// TODO(vicb): Create a benchmark for the different methods & the usage of the queue
4+
// see https://github.com/angular/zone.js/issues/97
5+
6+
// It is required to initialize hasNativePromise before requiring es6-promise otherwise es6-promise would
7+
// overwrite the native Promise implementation on v8 and the check would always return false.
8+
// see https://github.com/jakearchibald/es6-promise/issues/140
9+
var hasNativePromise = typeof Promise !== "undefined" &&
10+
Promise.toString().indexOf("[native code]") !== -1;
11+
12+
var isFirefox = global.navigator &&
13+
global.navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
14+
15+
var resolvedPromise;
16+
17+
// TODO(vicb): remove '!isFirefox' when the bug gets fixed:
18+
// https://bugzilla.mozilla.org/show_bug.cgi?id=1162013
19+
if (hasNativePromise && !isFirefox) {
20+
// When available use a native Promise to schedule microtasks.
21+
// When not available, es6-promise fallback will be used
22+
resolvedPromise = Promise.resolve();
23+
}
24+
325
var es6Promise = require('es6-promise').Promise;
426

27+
if (resolvedPromise) {
28+
es6Promise._setScheduler(function(fn) {
29+
resolvedPromise.then(fn);
30+
});
31+
}
32+
533
// es6-promise asap should schedule microtasks via zone.scheduleMicrotask so that any
634
// user defined hooks are triggered
735
es6Promise._setAsap(function(fn, arg) {
@@ -25,23 +53,5 @@ module.exports = {
2553
addMicrotaskSupport: addMicrotaskSupport
2654
};
2755

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

31-
var hasNativePromise = typeof Promise !== "undefined" &&
32-
Promise.toString().indexOf("[native code]") !== -1;
33-
34-
var isFirefox = global.navigator &&
35-
global.navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
36-
37-
// TODO(vicb): remove '!isFirefox' when the bug gets fixed:
38-
// https://bugzilla.mozilla.org/show_bug.cgi?id=1162013
39-
if (hasNativePromise && !isFirefox) {
40-
// When available use a native Promise to schedule microtasks.
41-
// When not available, es6-promise fallback will be used
42-
var resolvedPromise = Promise.resolve();
43-
es6Promise._setScheduler(function(fn) {
44-
resolvedPromise.then(fn);
45-
});
46-
}
4757

0 commit comments

Comments
 (0)