promise.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. (function(v, w) {
  2. "object" === typeof exports && "undefined" !== typeof module
  3. ? (module.exports = w())
  4. : "function" === typeof define && define.amd
  5. ? define(w)
  6. : (v.ES6Promise = w());
  7. })(this, function() {
  8. function v(a) {
  9. return "function" === typeof a;
  10. }
  11. function w() {
  12. return function() {
  13. return process.nextTick(n);
  14. };
  15. }
  16. function R() {
  17. return "undefined" !== typeof B
  18. ? function() {
  19. B(n);
  20. }
  21. : C();
  22. }
  23. function S() {
  24. var a = 0,
  25. b = new J(n),
  26. c = document.createTextNode("");
  27. b.observe(c, { characterData: !0 });
  28. return function() {
  29. c.data = a = ++a % 2;
  30. };
  31. }
  32. function T() {
  33. var a = new MessageChannel();
  34. a.port1.onmessage = n;
  35. return function() {
  36. return a.port2.postMessage(0);
  37. };
  38. }
  39. function C() {
  40. var a = setTimeout;
  41. return function() {
  42. return a(n, 1);
  43. };
  44. }
  45. function n() {
  46. for (var a = 0; a < k; a += 2)
  47. (0, q[a])(q[a + 1]), (q[a] = void 0), (q[a + 1] = void 0);
  48. k = 0;
  49. }
  50. function U() {
  51. try {
  52. var a = Function("return this")().require("vertx");
  53. B = a.runOnLoop || a.runOnContext;
  54. return R();
  55. } catch (b) {
  56. return C();
  57. }
  58. }
  59. function D(a, b) {
  60. var c = this,
  61. d = new this.constructor(r);
  62. void 0 === d[z] && K(d);
  63. var e = c._state;
  64. if (e) {
  65. var f = arguments[e - 1];
  66. l(function() {
  67. return L(e, d, f, c._result);
  68. });
  69. } else E(c, d, a, b);
  70. return d;
  71. }
  72. function F(a) {
  73. if (a && "object" === typeof a && a.constructor === this) return a;
  74. var b = new this(r);
  75. x(b, a);
  76. return b;
  77. }
  78. function r() {}
  79. function M(a) {
  80. try {
  81. return a.then;
  82. } catch (b) {
  83. return (p.error = b), p;
  84. }
  85. }
  86. function V(a, b, c, d) {
  87. try {
  88. a.call(b, c, d);
  89. } catch (e) {
  90. return e;
  91. }
  92. }
  93. function W(a, b, c) {
  94. l(function(a) {
  95. var e = !1,
  96. f = V(
  97. c,
  98. b,
  99. function(c) {
  100. e || ((e = !0), b !== c ? x(a, c) : m(a, c));
  101. },
  102. function(b) {
  103. e || ((e = !0), g(a, b));
  104. },
  105. "Settle: " + (a._label || " unknown promise")
  106. );
  107. !e && f && ((e = !0), g(a, f));
  108. }, a);
  109. }
  110. function X(a, b) {
  111. b._state === y
  112. ? m(a, b._result)
  113. : b._state === t
  114. ? g(a, b._result)
  115. : E(
  116. b,
  117. void 0,
  118. function(b) {
  119. return x(a, b);
  120. },
  121. function(b) {
  122. return g(a, b);
  123. }
  124. );
  125. }
  126. function N(a, b, c) {
  127. b.constructor === a.constructor && c === D && b.constructor.resolve === F
  128. ? X(a, b)
  129. : c === p
  130. ? (g(a, p.error), (p.error = null))
  131. : void 0 === c
  132. ? m(a, b)
  133. : v(c)
  134. ? W(a, b, c)
  135. : m(a, b);
  136. }
  137. function x(a, b) {
  138. if (a === b)
  139. g(a, new TypeError("You cannot resolve a promise with itself"));
  140. else {
  141. var c = typeof b;
  142. null === b || ("object" !== c && "function" !== c)
  143. ? m(a, b)
  144. : N(a, b, M(b));
  145. }
  146. }
  147. function Y(a) {
  148. a._onerror && a._onerror(a._result);
  149. G(a);
  150. }
  151. function m(a, b) {
  152. a._state === u &&
  153. ((a._result = b), (a._state = y), 0 !== a._subscribers.length && l(G, a));
  154. }
  155. function g(a, b) {
  156. a._state === u && ((a._state = t), (a._result = b), l(Y, a));
  157. }
  158. function E(a, b, c, d) {
  159. var e = a._subscribers,
  160. f = e.length;
  161. a._onerror = null;
  162. e[f] = b;
  163. e[f + y] = c;
  164. e[f + t] = d;
  165. 0 === f && a._state && l(G, a);
  166. }
  167. function G(a) {
  168. var b = a._subscribers,
  169. c = a._state;
  170. if (0 !== b.length) {
  171. for (
  172. var d = void 0, e = void 0, f = a._result, g = 0;
  173. g < b.length;
  174. g += 3
  175. )
  176. (d = b[g]), (e = b[g + c]), d ? L(c, d, e, f) : e(f);
  177. a._subscribers.length = 0;
  178. }
  179. }
  180. function L(a, b, c, d) {
  181. var e = v(c),
  182. f = void 0,
  183. h = void 0,
  184. k = void 0,
  185. l = void 0;
  186. if (e) {
  187. try {
  188. f = c(d);
  189. } catch (n) {
  190. (p.error = n), (f = p);
  191. }
  192. f === p ? ((l = !0), (h = f.error), (f.error = null)) : (k = !0);
  193. if (b === f) {
  194. g(
  195. b,
  196. new TypeError("A promises callback cannot return that same promise.")
  197. );
  198. return;
  199. }
  200. } else (f = d), (k = !0);
  201. b._state === u &&
  202. (e && k ? x(b, f) : l ? g(b, h) : a === y ? m(b, f) : a === t && g(b, f));
  203. }
  204. function Z(a, b) {
  205. try {
  206. b(
  207. function(b) {
  208. x(a, b);
  209. },
  210. function(b) {
  211. g(a, b);
  212. }
  213. );
  214. } catch (c) {
  215. g(a, c);
  216. }
  217. }
  218. function K(a) {
  219. a[z] = O++;
  220. a._state = void 0;
  221. a._result = void 0;
  222. a._subscribers = [];
  223. }
  224. var H = void 0,
  225. P = (H = Array.isArray
  226. ? Array.isArray
  227. : function(a) {
  228. return "[object Array]" === Object.prototype.toString.call(a);
  229. }),
  230. k = 0,
  231. B = void 0,
  232. I = void 0,
  233. l = function(a, b) {
  234. q[k] = a;
  235. q[k + 1] = b;
  236. k += 2;
  237. 2 === k && (I ? I(n) : Q());
  238. },
  239. A = (H = "undefined" !== typeof window ? window : void 0) || {},
  240. J = A.MutationObserver || A.WebKitMutationObserver,
  241. A =
  242. "undefined" === typeof self &&
  243. "undefined" !== typeof process &&
  244. "[object process]" === {}.toString.call(process),
  245. aa =
  246. "undefined" !== typeof Uint8ClampedArray &&
  247. "undefined" !== typeof importScripts &&
  248. "undefined" !== typeof MessageChannel,
  249. q = Array(1e3),
  250. Q = void 0,
  251. Q = A
  252. ? w()
  253. : J
  254. ? S()
  255. : aa
  256. ? T()
  257. : void 0 === H && "function" === typeof require
  258. ? U()
  259. : C(),
  260. z = Math.random()
  261. .toString(36)
  262. .substring(2),
  263. u = void 0,
  264. y = 1,
  265. t = 2,
  266. p = { error: null },
  267. O = 0,
  268. ba = (function() {
  269. function a(a, c) {
  270. this._instanceConstructor = a;
  271. this.promise = new a(r);
  272. this.promise[z] || K(this.promise);
  273. P(c)
  274. ? ((this._remaining = this.length = c.length),
  275. (this._result = Array(this.length)),
  276. 0 === this.length
  277. ? m(this.promise, this._result)
  278. : ((this.length = this.length || 0),
  279. this._enumerate(c),
  280. 0 === this._remaining && m(this.promise, this._result)))
  281. : g(this.promise, Error("Array Methods must be provided an Array"));
  282. }
  283. a.prototype._enumerate = function(a) {
  284. for (var c = 0; this._state === u && c < a.length; c++)
  285. this._eachEntry(a[c], c);
  286. };
  287. a.prototype._eachEntry = function(a, c) {
  288. var d = this._instanceConstructor,
  289. e = d.resolve;
  290. e === F
  291. ? ((e = M(a)),
  292. e === D && a._state !== u
  293. ? this._settledAt(a._state, c, a._result)
  294. : "function" !== typeof e
  295. ? (this._remaining--, (this._result[c] = a))
  296. : d === h
  297. ? ((d = new d(r)), N(d, a, e), this._willSettleAt(d, c))
  298. : this._willSettleAt(
  299. new d(function(c) {
  300. return c(a);
  301. }),
  302. c
  303. ))
  304. : this._willSettleAt(e(a), c);
  305. };
  306. a.prototype._settledAt = function(a, c, d) {
  307. var e = this.promise;
  308. e._state === u &&
  309. (this._remaining--, a === t ? g(e, d) : (this._result[c] = d));
  310. 0 === this._remaining && m(e, this._result);
  311. };
  312. a.prototype._willSettleAt = function(a, c) {
  313. var d = this;
  314. E(
  315. a,
  316. void 0,
  317. function(a) {
  318. return d._settledAt(y, c, a);
  319. },
  320. function(a) {
  321. return d._settledAt(t, c, a);
  322. }
  323. );
  324. };
  325. return a;
  326. })(),
  327. h = (function() {
  328. function a(b) {
  329. this[z] = O++;
  330. this._result = this._state = void 0;
  331. this._subscribers = [];
  332. if (r !== b) {
  333. if ("function" !== typeof b)
  334. throw new TypeError(
  335. "You must pass a resolver function as the first argument to the promise constructor"
  336. );
  337. if (this instanceof a) Z(this, b);
  338. else
  339. throw new TypeError(
  340. "Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function."
  341. );
  342. }
  343. }
  344. a.prototype["catch"] = function(a) {
  345. return this.then(null, a);
  346. };
  347. a.prototype["finally"] = function(a) {
  348. var c = this.constructor;
  349. return v(a)
  350. ? this.then(
  351. function(d) {
  352. return c.resolve(a()).then(function() {
  353. return d;
  354. });
  355. },
  356. function(d) {
  357. return c.resolve(a()).then(function() {
  358. throw d;
  359. });
  360. }
  361. )
  362. : this.then(a, a);
  363. };
  364. return a;
  365. })();
  366. h.prototype.then = D;
  367. h.all = function(a) {
  368. return new ba(this, a).promise;
  369. };
  370. h.race = function(a) {
  371. var b = this;
  372. return P(a)
  373. ? new b(function(c, d) {
  374. for (var e = a.length, f = 0; f < e; f++) b.resolve(a[f]).then(c, d);
  375. })
  376. : new b(function(a, b) {
  377. return b(new TypeError("You must pass an array to race."));
  378. });
  379. };
  380. h.resolve = F;
  381. h.reject = function(a) {
  382. var b = new this(r);
  383. g(b, a);
  384. return b;
  385. };
  386. h._setScheduler = function(a) {
  387. I = a;
  388. };
  389. h._setAsap = function(a) {
  390. l = a;
  391. };
  392. h._asap = l;
  393. h.polyfill = function() {
  394. var a = void 0;
  395. if ("undefined" !== typeof global) a = global;
  396. else if ("undefined" !== typeof self) a = self;
  397. else
  398. try {
  399. a = Function("return this")();
  400. } catch (b) {
  401. throw Error(
  402. "polyfill failed because global object is unavailable in this environment"
  403. );
  404. }
  405. var c = a.Promise;
  406. if (c) {
  407. var d = null;
  408. try {
  409. d = Object.prototype.toString.call(c.resolve());
  410. } catch (e) {}
  411. if ("[object Promise]" === d && !c.cast) return;
  412. }
  413. a.Promise = h;
  414. };
  415. return (h.Promise = h);
  416. });