Promise.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. var t = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function(t) {
  2. return typeof t;
  3. } : function(t) {
  4. return t && "function" == typeof Symbol && t.constructor === Symbol && t !== Symbol.prototype ? "symbol" : typeof t;
  5. };
  6. !function(e, n) {
  7. "object" == ("undefined" == typeof exports ? "undefined" : t(exports)) && "undefined" != typeof module ? module.exports = n() : "function" == typeof define && define.amd ? define(n) : e.ES6Promise = n();
  8. }(void 0, function() {
  9. function e(e) {
  10. var n = void 0 === e ? "undefined" : t(e);
  11. return null !== e && ("object" === n || "function" === n);
  12. }
  13. function n(t) {
  14. return "function" == typeof t;
  15. }
  16. function r() {
  17. return void 0 !== D ? function() {
  18. D(i);
  19. } : o();
  20. }
  21. function o() {
  22. var t = setTimeout;
  23. return function() {
  24. return t(i, 1);
  25. };
  26. }
  27. function i() {
  28. for (var t = 0; t < F; t += 2) (0, G[t])(G[t + 1]), G[t] = void 0, G[t + 1] = void 0;
  29. F = 0;
  30. }
  31. function s(t, e) {
  32. var n = arguments, r = this, o = new this.constructor(c);
  33. void 0 === o[I] && x(o);
  34. var i = r._state;
  35. return i ? function() {
  36. var t = n[i - 1];
  37. L(function() {
  38. return j(i, o, t, r._result);
  39. });
  40. }() : w(r, o, t, e), o;
  41. }
  42. function u(e) {
  43. var n = this;
  44. if (e && "object" == (void 0 === e ? "undefined" : t(e)) && e.constructor === n) return e;
  45. var r = new n(c);
  46. return _(r, e), r;
  47. }
  48. function c() {}
  49. function f() {
  50. return new TypeError("You cannot resolve a promise with itself");
  51. }
  52. function a() {
  53. return new TypeError("A promises callback cannot return that same promise.");
  54. }
  55. function l(t) {
  56. try {
  57. return t.then;
  58. } catch (t) {
  59. return V.error = t, V;
  60. }
  61. }
  62. function h(t, e, n, r) {
  63. try {
  64. t.call(e, n, r);
  65. } catch (t) {
  66. return t;
  67. }
  68. }
  69. function d(t, e, n) {
  70. L(function(t) {
  71. var r = !1, o = h(n, e, function(n) {
  72. r || (r = !0, e !== n ? _(t, n) : m(t, n));
  73. }, function(e) {
  74. r || (r = !0, b(t, e));
  75. }, "Settle: " + (t._label || " unknown promise"));
  76. !r && o && (r = !0, b(t, o));
  77. }, t);
  78. }
  79. function p(t, e) {
  80. e._state === Q ? m(t, e._result) : e._state === R ? b(t, e._result) : w(e, void 0, function(e) {
  81. return _(t, e);
  82. }, function(e) {
  83. return b(t, e);
  84. });
  85. }
  86. function v(t, e, r) {
  87. e.constructor === t.constructor && r === s && e.constructor.resolve === u ? p(t, e) : r === V ? (b(t, V.error),
  88. V.error = null) : void 0 === r ? m(t, e) : n(r) ? d(t, e, r) : m(t, e);
  89. }
  90. function _(t, n) {
  91. t === n ? b(t, f()) : e(n) ? v(t, n, l(n)) : m(t, n);
  92. }
  93. function y(t) {
  94. t._onerror && t._onerror(t._result), g(t);
  95. }
  96. function m(t, e) {
  97. t._state === J && (t._result = e, t._state = Q, 0 !== t._subscribers.length && L(g, t));
  98. }
  99. function b(t, e) {
  100. t._state === J && (t._state = R, t._result = e, L(y, t));
  101. }
  102. function w(t, e, n, r) {
  103. var o = t._subscribers, i = o.length;
  104. t._onerror = null, o[i] = e, o[i + Q] = n, o[i + R] = r, 0 === i && t._state && L(g, t);
  105. }
  106. function g(t) {
  107. var e = t._subscribers, n = t._state;
  108. if (0 !== e.length) {
  109. for (var r = void 0, o = void 0, i = t._result, s = 0; s < e.length; s += 3) r = e[s],
  110. o = e[s + n], r ? j(n, r, o, i) : o(i);
  111. t._subscribers.length = 0;
  112. }
  113. }
  114. function A() {
  115. this.error = null;
  116. }
  117. function S(t, e) {
  118. try {
  119. return t(e);
  120. } catch (t) {
  121. return X.error = t, X;
  122. }
  123. }
  124. function j(t, e, r, o) {
  125. var i = n(r), s = void 0, u = void 0, c = void 0, f = void 0;
  126. if (i) {
  127. if ((s = S(r, o)) === X ? (f = !0, u = s.error, s.error = null) : c = !0, e === s) return void b(e, a());
  128. } else s = o, c = !0;
  129. e._state !== J || (i && c ? _(e, s) : f ? b(e, u) : t === Q ? m(e, s) : t === R && b(e, s));
  130. }
  131. function E(t, e) {
  132. try {
  133. e(function(e) {
  134. _(t, e);
  135. }, function(e) {
  136. b(t, e);
  137. });
  138. } catch (e) {
  139. b(t, e);
  140. }
  141. }
  142. function T() {
  143. return Z++;
  144. }
  145. function x(t) {
  146. t[I] = Z++, t._state = void 0, t._result = void 0, t._subscribers = [];
  147. }
  148. function M(t, e) {
  149. this._instanceConstructor = t, this.promise = new t(c), this.promise[I] || x(this.promise),
  150. q(e) ? (this.length = e.length, this._remaining = e.length, this._result = new Array(this.length),
  151. 0 === this.length ? m(this.promise, this._result) : (this.length = this.length || 0,
  152. this._enumerate(e), 0 === this._remaining && m(this.promise, this._result))) : b(this.promise, P());
  153. }
  154. function P() {
  155. return new Error("Array Methods must be provided an Array");
  156. }
  157. function C() {
  158. throw new TypeError("You must pass a resolver function as the first argument to the promise constructor");
  159. }
  160. function O() {
  161. throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.");
  162. }
  163. function k(t) {
  164. this[I] = T(), this._result = this._state = void 0, this._subscribers = [], c !== t && ("function" != typeof t && C(),
  165. this instanceof k ? E(this, t) : O());
  166. }
  167. var Y = void 0, q = Y = Array.isArray ? Array.isArray : function(t) {
  168. return "[object Array]" === Object.prototype.toString.call(t);
  169. }, F = 0, D = void 0, K = void 0, L = function(t, e) {
  170. G[F] = t, G[F + 1] = e, 2 === (F += 2) && (K ? K(i) : H());
  171. }, N = "undefined" != typeof window ? window : void 0, U = N || {}, W = U.MutationObserver || U.WebKitMutationObserver, z = "undefined" == typeof self && "undefined" != typeof process && "[object process]" === {}.toString.call(process), B = "undefined" != typeof Uint8ClampedArray && "undefined" != typeof importScripts && "undefined" != typeof MessageChannel, G = new Array(1e3), H = void 0;
  172. H = z ? function() {
  173. return process.nextTick(i);
  174. } : W ? function() {
  175. var t = 0, e = new W(i), n = document.createTextNode("");
  176. return e.observe(n, {
  177. characterData: !0
  178. }), function() {
  179. n.data = t = ++t % 2;
  180. };
  181. }() : B ? function() {
  182. var t = new MessageChannel();
  183. return t.port1.onmessage = i, function() {
  184. return t.port2.postMessage(0);
  185. };
  186. }() : void 0 === N && "function" == typeof require ? function() {
  187. try {
  188. var t = require("vertx");
  189. return D = t.runOnLoop || t.runOnContext, r();
  190. } catch (t) {
  191. return o();
  192. }
  193. }() : o();
  194. var I = Math.random().toString(36).substring(16), J = void 0, Q = 1, R = 2, V = new A(), X = new A(), Z = 0;
  195. return M.prototype._enumerate = function(t) {
  196. for (var e = 0; this._state === J && e < t.length; e++) this._eachEntry(t[e], e);
  197. }, M.prototype._eachEntry = function(t, e) {
  198. var n = this._instanceConstructor, r = n.resolve;
  199. if (r === u) {
  200. var o = l(t);
  201. if (o === s && t._state !== J) this._settledAt(t._state, e, t._result); else if ("function" != typeof o) this._remaining--,
  202. this._result[e] = t; else if (n === k) {
  203. var i = new n(c);
  204. v(i, t, o), this._willSettleAt(i, e);
  205. } else this._willSettleAt(new n(function(e) {
  206. return e(t);
  207. }), e);
  208. } else this._willSettleAt(r(t), e);
  209. }, M.prototype._settledAt = function(t, e, n) {
  210. var r = this.promise;
  211. r._state === J && (this._remaining--, t === R ? b(r, n) : this._result[e] = n),
  212. 0 === this._remaining && m(r, this._result);
  213. }, M.prototype._willSettleAt = function(t, e) {
  214. var n = this;
  215. w(t, void 0, function(t) {
  216. return n._settledAt(Q, e, t);
  217. }, function(t) {
  218. return n._settledAt(R, e, t);
  219. });
  220. }, k.all = function(t) {
  221. return new M(this, t).promise;
  222. }, k.race = function(t) {
  223. var e = this;
  224. return new e(q(t) ? function(n, r) {
  225. for (var o = t.length, i = 0; i < o; i++) e.resolve(t[i]).then(n, r);
  226. } : function(t, e) {
  227. return e(new TypeError("You must pass an array to race."));
  228. });
  229. }, k.resolve = u, k.reject = function(t) {
  230. var e = new this(c);
  231. return b(e, t), e;
  232. }, k._setScheduler = function(t) {
  233. K = t;
  234. }, k._setAsap = function(t) {
  235. L = t;
  236. }, k._asap = L, k.prototype = {
  237. constructor: k,
  238. then: s,
  239. catch: function(t) {
  240. return this.then(null, t);
  241. }
  242. }, k.polyfill = function() {
  243. var t = void 0;
  244. if ("undefined" != typeof global) t = global; else if ("undefined" != typeof self) t = self; else try {
  245. t = Function("return this")();
  246. } catch (t) {
  247. throw new Error("polyfill failed because global object is unavailable in this environment");
  248. }
  249. var e = t.Promise;
  250. if (e) {
  251. var n = null;
  252. try {
  253. n = Object.prototype.toString.call(e.resolve());
  254. } catch (t) {}
  255. if ("[object Promise]" === n && !e.cast) return;
  256. }
  257. t.Promise = k;
  258. }, k.Promise = k, k;
  259. });