promise.js
1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"use strict";
/* global self, window, module, global, require */
module.exports = function () {
"use strict";
var globalObject = void 0;
function isFunction(x) {
return typeof x === "function";
}
// Seek the global object
if (global !== undefined) {
globalObject = global;
} else if (window !== undefined && window.document) {
globalObject = window;
} else {
globalObject = self;
}
// Test for any native promise implementation, and if that
// implementation appears to conform to the specificaton.
// This code mostly nicked from the es6-promise module polyfill
// and then fooled with.
var hasPromiseSupport = function () {
// No promise object at all, and it's a non-starter
if (!globalObject.hasOwnProperty("Promise")) {
return false;
}
// There is a Promise object. Does it conform to the spec?
var P = globalObject.Promise;
// Some of these methods are missing from
// Firefox/Chrome experimental implementations
if (!P.hasOwnProperty("resolve") || !P.hasOwnProperty("reject")) {
return false;
}
if (!P.hasOwnProperty("all") || !P.hasOwnProperty("race")) {
return false;
}
// Older version of the spec had a resolver object
// as the arg rather than a function
return function () {
var resolve = void 0;
var p = new globalObject.Promise(function (r) {
resolve = r;
});
if (p) {
return isFunction(resolve);
}
return false;
}();
}();
// Export the native Promise implementation if it
// looks like it matches the spec
if (hasPromiseSupport) {
return globalObject.Promise;
}
// Otherwise, return the es6-promise polyfill by @jaffathecake.
return require("es6-promise").Promise;
}();