Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

promise.js 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. (function() {
  2. var define, requireModule, require, requirejs;
  3. (function() {
  4. var registry = {}, seen = {};
  5. define = function(name, deps, callback) {
  6. registry[name] = { deps: deps, callback: callback };
  7. };
  8. requirejs = require = requireModule = function(name) {
  9. requirejs._eak_seen = registry;
  10. if (seen[name]) { return seen[name]; }
  11. seen[name] = {};
  12. if (!registry[name]) {
  13. throw new Error("Could not find module " + name);
  14. }
  15. var mod = registry[name],
  16. deps = mod.deps,
  17. callback = mod.callback,
  18. reified = [],
  19. exports;
  20. for (var i=0, l=deps.length; i<l; i++) {
  21. if (deps[i] === 'exports') {
  22. reified.push(exports = {});
  23. } else {
  24. reified.push(requireModule(resolve(deps[i])));
  25. }
  26. }
  27. var value = callback.apply(this, reified);
  28. return seen[name] = exports || value;
  29. function resolve(child) {
  30. if (child.charAt(0) !== '.') { return child; }
  31. var parts = child.split("/");
  32. var parentBase = name.split("/").slice(0, -1);
  33. for (var i=0, l=parts.length; i<l; i++) {
  34. var part = parts[i];
  35. if (part === '..') { parentBase.pop(); }
  36. else if (part === '.') { continue; }
  37. else { parentBase.push(part); }
  38. }
  39. return parentBase.join("/");
  40. }
  41. };
  42. })();
  43. define("promise/all",
  44. ["./utils","exports"],
  45. function(__dependency1__, __exports__) {
  46. "use strict";
  47. /* global toString */
  48. var isArray = __dependency1__.isArray;
  49. var isFunction = __dependency1__.isFunction;
  50. /**
  51. Returns a promise that is fulfilled when all the given promises have been
  52. fulfilled, or rejected if any of them become rejected. The return promise
  53. is fulfilled with an array that gives all the values in the order they were
  54. passed in the `promises` array argument.
  55. Example:
  56. ```javascript
  57. var promise1 = RSVP.resolve(1);
  58. var promise2 = RSVP.resolve(2);
  59. var promise3 = RSVP.resolve(3);
  60. var promises = [ promise1, promise2, promise3 ];
  61. RSVP.all(promises).then(function(array){
  62. // The array here would be [ 1, 2, 3 ];
  63. });
  64. ```
  65. If any of the `promises` given to `RSVP.all` are rejected, the first promise
  66. that is rejected will be given as an argument to the returned promises's
  67. rejection handler. For example:
  68. Example:
  69. ```javascript
  70. var promise1 = RSVP.resolve(1);
  71. var promise2 = RSVP.reject(new Error("2"));
  72. var promise3 = RSVP.reject(new Error("3"));
  73. var promises = [ promise1, promise2, promise3 ];
  74. RSVP.all(promises).then(function(array){
  75. // Code here never runs because there are rejected promises!
  76. }, function(error) {
  77. // error.message === "2"
  78. });
  79. ```
  80. @method all
  81. @for RSVP
  82. @param {Array} promises
  83. @param {String} label
  84. @return {Promise} promise that is fulfilled when all `promises` have been
  85. fulfilled, or rejected if any of them become rejected.
  86. */
  87. function all(promises) {
  88. /*jshint validthis:true */
  89. var Promise = this;
  90. if (!isArray(promises)) {
  91. throw new TypeError('You must pass an array to all.');
  92. }
  93. return new Promise(function(resolve, reject) {
  94. var results = [], remaining = promises.length,
  95. promise;
  96. if (remaining === 0) {
  97. resolve([]);
  98. }
  99. function resolver(index) {
  100. return function(value) {
  101. resolveAll(index, value);
  102. };
  103. }
  104. function resolveAll(index, value) {
  105. results[index] = value;
  106. if (--remaining === 0) {
  107. resolve(results);
  108. }
  109. }
  110. for (var i = 0; i < promises.length; i++) {
  111. promise = promises[i];
  112. if (promise && isFunction(promise.then)) {
  113. promise.then(resolver(i), reject);
  114. } else {
  115. resolveAll(i, promise);
  116. }
  117. }
  118. });
  119. }
  120. __exports__.all = all;
  121. });
  122. define("promise/asap",
  123. ["exports"],
  124. function(__exports__) {
  125. "use strict";
  126. var browserGlobal = (typeof window !== 'undefined') ? window : {};
  127. var BrowserMutationObserver = browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver;
  128. var local = (typeof global !== 'undefined') ? global : (this === undefined? window:this);
  129. // node
  130. function useNextTick() {
  131. return function() {
  132. process.nextTick(flush);
  133. };
  134. }
  135. function useMutationObserver() {
  136. var iterations = 0;
  137. var observer = new BrowserMutationObserver(flush);
  138. var node = document.createTextNode('');
  139. observer.observe(node, { characterData: true });
  140. return function() {
  141. node.data = (iterations = ++iterations % 2);
  142. };
  143. }
  144. function useSetTimeout() {
  145. return function() {
  146. local.setTimeout(flush, 1);
  147. };
  148. }
  149. var queue = [];
  150. function flush() {
  151. for (var i = 0; i < queue.length; i++) {
  152. var tuple = queue[i];
  153. var callback = tuple[0], arg = tuple[1];
  154. callback(arg);
  155. }
  156. queue = [];
  157. }
  158. var scheduleFlush;
  159. // Decide what async method to use to triggering processing of queued callbacks:
  160. if (typeof process !== 'undefined' && {}.toString.call(process) === '[object process]') {
  161. scheduleFlush = useNextTick();
  162. } else if (BrowserMutationObserver) {
  163. scheduleFlush = useMutationObserver();
  164. } else {
  165. scheduleFlush = useSetTimeout();
  166. }
  167. function asap(callback, arg) {
  168. var length = queue.push([callback, arg]);
  169. if (length === 1) {
  170. // If length is 1, that means that we need to schedule an async flush.
  171. // If additional callbacks are queued before the queue is flushed, they
  172. // will be processed by this flush that we are scheduling.
  173. scheduleFlush();
  174. }
  175. }
  176. __exports__.asap = asap;
  177. });
  178. define("promise/config",
  179. ["exports"],
  180. function(__exports__) {
  181. "use strict";
  182. var config = {
  183. instrument: false
  184. };
  185. function configure(name, value) {
  186. if (arguments.length === 2) {
  187. config[name] = value;
  188. } else {
  189. return config[name];
  190. }
  191. }
  192. __exports__.config = config;
  193. __exports__.configure = configure;
  194. });
  195. define("promise/polyfill",
  196. ["./promise","./utils","exports"],
  197. function(__dependency1__, __dependency2__, __exports__) {
  198. "use strict";
  199. /*global self*/
  200. var RSVPPromise = __dependency1__.Promise;
  201. var isFunction = __dependency2__.isFunction;
  202. function polyfill() {
  203. var local;
  204. if (typeof global !== 'undefined') {
  205. local = global;
  206. } else if (typeof window !== 'undefined' && window.document) {
  207. local = window;
  208. } else {
  209. local = self;
  210. }
  211. var es6PromiseSupport =
  212. "Promise" in local &&
  213. // Some of these methods are missing from
  214. // Firefox/Chrome experimental implementations
  215. "resolve" in local.Promise &&
  216. "reject" in local.Promise &&
  217. "all" in local.Promise &&
  218. "race" in local.Promise &&
  219. // Older version of the spec had a resolver object
  220. // as the arg rather than a function
  221. (function() {
  222. var resolve;
  223. new local.Promise(function(r) { resolve = r; });
  224. return isFunction(resolve);
  225. }());
  226. if (!es6PromiseSupport) {
  227. local.Promise = RSVPPromise;
  228. }
  229. }
  230. __exports__.polyfill = polyfill;
  231. });
  232. define("promise/promise",
  233. ["./config","./utils","./all","./race","./resolve","./reject","./asap","exports"],
  234. function(__dependency1__, __dependency2__, __dependency3__, __dependency4__, __dependency5__, __dependency6__, __dependency7__, __exports__) {
  235. "use strict";
  236. var config = __dependency1__.config;
  237. var configure = __dependency1__.configure;
  238. var objectOrFunction = __dependency2__.objectOrFunction;
  239. var isFunction = __dependency2__.isFunction;
  240. var now = __dependency2__.now;
  241. var all = __dependency3__.all;
  242. var race = __dependency4__.race;
  243. var staticResolve = __dependency5__.resolve;
  244. var staticReject = __dependency6__.reject;
  245. var asap = __dependency7__.asap;
  246. var counter = 0;
  247. config.async = asap; // default async is asap;
  248. function Promise(resolver) {
  249. if (!isFunction(resolver)) {
  250. throw new TypeError('You must pass a resolver function as the first argument to the promise constructor');
  251. }
  252. if (!(this instanceof Promise)) {
  253. throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.");
  254. }
  255. this._subscribers = [];
  256. invokeResolver(resolver, this);
  257. }
  258. function invokeResolver(resolver, promise) {
  259. function resolvePromise(value) {
  260. resolve(promise, value);
  261. }
  262. function rejectPromise(reason) {
  263. reject(promise, reason);
  264. }
  265. try {
  266. resolver(resolvePromise, rejectPromise);
  267. } catch(e) {
  268. rejectPromise(e);
  269. }
  270. }
  271. function invokeCallback(settled, promise, callback, detail) {
  272. var hasCallback = isFunction(callback),
  273. value, error, succeeded, failed;
  274. if (hasCallback) {
  275. try {
  276. value = callback(detail);
  277. succeeded = true;
  278. } catch(e) {
  279. failed = true;
  280. error = e;
  281. }
  282. } else {
  283. value = detail;
  284. succeeded = true;
  285. }
  286. if (handleThenable(promise, value)) {
  287. return;
  288. } else if (hasCallback && succeeded) {
  289. resolve(promise, value);
  290. } else if (failed) {
  291. reject(promise, error);
  292. } else if (settled === FULFILLED) {
  293. resolve(promise, value);
  294. } else if (settled === REJECTED) {
  295. reject(promise, value);
  296. }
  297. }
  298. var PENDING = void 0;
  299. var SEALED = 0;
  300. var FULFILLED = 1;
  301. var REJECTED = 2;
  302. function subscribe(parent, child, onFulfillment, onRejection) {
  303. var subscribers = parent._subscribers;
  304. var length = subscribers.length;
  305. subscribers[length] = child;
  306. subscribers[length + FULFILLED] = onFulfillment;
  307. subscribers[length + REJECTED] = onRejection;
  308. }
  309. function publish(promise, settled) {
  310. var child, callback, subscribers = promise._subscribers, detail = promise._detail;
  311. for (var i = 0; i < subscribers.length; i += 3) {
  312. child = subscribers[i];
  313. callback = subscribers[i + settled];
  314. invokeCallback(settled, child, callback, detail);
  315. }
  316. promise._subscribers = null;
  317. }
  318. Promise.prototype = {
  319. constructor: Promise,
  320. _state: undefined,
  321. _detail: undefined,
  322. _subscribers: undefined,
  323. then: function(onFulfillment, onRejection) {
  324. var promise = this;
  325. var thenPromise = new this.constructor(function() {});
  326. if (this._state) {
  327. var callbacks = arguments;
  328. config.async(function invokePromiseCallback() {
  329. invokeCallback(promise._state, thenPromise, callbacks[promise._state - 1], promise._detail);
  330. });
  331. } else {
  332. subscribe(this, thenPromise, onFulfillment, onRejection);
  333. }
  334. return thenPromise;
  335. },
  336. 'catch': function(onRejection) {
  337. return this.then(null, onRejection);
  338. }
  339. };
  340. Promise.all = all;
  341. Promise.race = race;
  342. Promise.resolve = staticResolve;
  343. Promise.reject = staticReject;
  344. function handleThenable(promise, value) {
  345. var then = null,
  346. resolved;
  347. try {
  348. if (promise === value) {
  349. throw new TypeError("A promises callback cannot return that same promise.");
  350. }
  351. if (objectOrFunction(value)) {
  352. then = value.then;
  353. if (isFunction(then)) {
  354. then.call(value, function(val) {
  355. if (resolved) { return true; }
  356. resolved = true;
  357. if (value !== val) {
  358. resolve(promise, val);
  359. } else {
  360. fulfill(promise, val);
  361. }
  362. }, function(val) {
  363. if (resolved) { return true; }
  364. resolved = true;
  365. reject(promise, val);
  366. });
  367. return true;
  368. }
  369. }
  370. } catch (error) {
  371. if (resolved) { return true; }
  372. reject(promise, error);
  373. return true;
  374. }
  375. return false;
  376. }
  377. function resolve(promise, value) {
  378. if (promise === value) {
  379. fulfill(promise, value);
  380. } else if (!handleThenable(promise, value)) {
  381. fulfill(promise, value);
  382. }
  383. }
  384. function fulfill(promise, value) {
  385. if (promise._state !== PENDING) { return; }
  386. promise._state = SEALED;
  387. promise._detail = value;
  388. config.async(publishFulfillment, promise);
  389. }
  390. function reject(promise, reason) {
  391. if (promise._state !== PENDING) { return; }
  392. promise._state = SEALED;
  393. promise._detail = reason;
  394. config.async(publishRejection, promise);
  395. }
  396. function publishFulfillment(promise) {
  397. publish(promise, promise._state = FULFILLED);
  398. }
  399. function publishRejection(promise) {
  400. publish(promise, promise._state = REJECTED);
  401. }
  402. __exports__.Promise = Promise;
  403. });
  404. define("promise/race",
  405. ["./utils","exports"],
  406. function(__dependency1__, __exports__) {
  407. "use strict";
  408. /* global toString */
  409. var isArray = __dependency1__.isArray;
  410. /**
  411. `RSVP.race` allows you to watch a series of promises and act as soon as the
  412. first promise given to the `promises` argument fulfills or rejects.
  413. Example:
  414. ```javascript
  415. var promise1 = new RSVP.Promise(function(resolve, reject){
  416. setTimeout(function(){
  417. resolve("promise 1");
  418. }, 200);
  419. });
  420. var promise2 = new RSVP.Promise(function(resolve, reject){
  421. setTimeout(function(){
  422. resolve("promise 2");
  423. }, 100);
  424. });
  425. RSVP.race([promise1, promise2]).then(function(result){
  426. // result === "promise 2" because it was resolved before promise1
  427. // was resolved.
  428. });
  429. ```
  430. `RSVP.race` is deterministic in that only the state of the first completed
  431. promise matters. For example, even if other promises given to the `promises`
  432. array argument are resolved, but the first completed promise has become
  433. rejected before the other promises became fulfilled, the returned promise
  434. will become rejected:
  435. ```javascript
  436. var promise1 = new RSVP.Promise(function(resolve, reject){
  437. setTimeout(function(){
  438. resolve("promise 1");
  439. }, 200);
  440. });
  441. var promise2 = new RSVP.Promise(function(resolve, reject){
  442. setTimeout(function(){
  443. reject(new Error("promise 2"));
  444. }, 100);
  445. });
  446. RSVP.race([promise1, promise2]).then(function(result){
  447. // Code here never runs because there are rejected promises!
  448. }, function(reason){
  449. // reason.message === "promise2" because promise 2 became rejected before
  450. // promise 1 became fulfilled
  451. });
  452. ```
  453. @method race
  454. @for RSVP
  455. @param {Array} promises array of promises to observe
  456. @param {String} label optional string for describing the promise returned.
  457. Useful for tooling.
  458. @return {Promise} a promise that becomes fulfilled with the value the first
  459. completed promises is resolved with if the first completed promise was
  460. fulfilled, or rejected with the reason that the first completed promise
  461. was rejected with.
  462. */
  463. function race(promises) {
  464. /*jshint validthis:true */
  465. var Promise = this;
  466. if (!isArray(promises)) {
  467. throw new TypeError('You must pass an array to race.');
  468. }
  469. return new Promise(function(resolve, reject) {
  470. var results = [], promise;
  471. for (var i = 0; i < promises.length; i++) {
  472. promise = promises[i];
  473. if (promise && typeof promise.then === 'function') {
  474. promise.then(resolve, reject);
  475. } else {
  476. resolve(promise);
  477. }
  478. }
  479. });
  480. }
  481. __exports__.race = race;
  482. });
  483. define("promise/reject",
  484. ["exports"],
  485. function(__exports__) {
  486. "use strict";
  487. /**
  488. `RSVP.reject` returns a promise that will become rejected with the passed
  489. `reason`. `RSVP.reject` is essentially shorthand for the following:
  490. ```javascript
  491. var promise = new RSVP.Promise(function(resolve, reject){
  492. reject(new Error('WHOOPS'));
  493. });
  494. promise.then(function(value){
  495. // Code here doesn't run because the promise is rejected!
  496. }, function(reason){
  497. // reason.message === 'WHOOPS'
  498. });
  499. ```
  500. Instead of writing the above, your code now simply becomes the following:
  501. ```javascript
  502. var promise = RSVP.reject(new Error('WHOOPS'));
  503. promise.then(function(value){
  504. // Code here doesn't run because the promise is rejected!
  505. }, function(reason){
  506. // reason.message === 'WHOOPS'
  507. });
  508. ```
  509. @method reject
  510. @for RSVP
  511. @param {Any} reason value that the returned promise will be rejected with.
  512. @param {String} label optional string for identifying the returned promise.
  513. Useful for tooling.
  514. @return {Promise} a promise that will become rejected with the given
  515. `reason`.
  516. */
  517. function reject(reason) {
  518. /*jshint validthis:true */
  519. var Promise = this;
  520. return new Promise(function (resolve, reject) {
  521. reject(reason);
  522. });
  523. }
  524. __exports__.reject = reject;
  525. });
  526. define("promise/resolve",
  527. ["exports"],
  528. function(__exports__) {
  529. "use strict";
  530. function resolve(value) {
  531. /*jshint validthis:true */
  532. if (value && typeof value === 'object' && value.constructor === this) {
  533. return value;
  534. }
  535. var Promise = this;
  536. return new Promise(function(resolve) {
  537. resolve(value);
  538. });
  539. }
  540. __exports__.resolve = resolve;
  541. });
  542. define("promise/utils",
  543. ["exports"],
  544. function(__exports__) {
  545. "use strict";
  546. function objectOrFunction(x) {
  547. return isFunction(x) || (typeof x === "object" && x !== null);
  548. }
  549. function isFunction(x) {
  550. return typeof x === "function";
  551. }
  552. function isArray(x) {
  553. return Object.prototype.toString.call(x) === "[object Array]";
  554. }
  555. // Date.now is not available in browsers < IE9
  556. // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/now#Compatibility
  557. var now = Date.now || function() { return new Date().getTime(); };
  558. __exports__.objectOrFunction = objectOrFunction;
  559. __exports__.isFunction = isFunction;
  560. __exports__.isArray = isArray;
  561. __exports__.now = now;
  562. });
  563. requireModule('promise/polyfill').polyfill();
  564. }());