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