You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

promise.js 1.9KB

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