Promise.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * This class provides an API compatible implementation of the ECMAScript 6 Promises API
  3. * (providing an implementation as necessary for browsers that do not natively support the
  4. * `Promise` class).
  5. *
  6. * This class will use the native `Promise` implementation if one is available. The
  7. * native implementation, while standard, does not provide all of the features of the
  8. * Ext JS Promises implementation.
  9. *
  10. * To use the Ext JS enhanced Promises implementation, see `{@link Ext.Deferred}` for
  11. * creating enhanced promises and additional static utility methods.
  12. *
  13. * Typical usage:
  14. *
  15. * function getAjax (url) {
  16. * // The function passed to Ext.Promise() is called immediately to start
  17. * // the asynchronous action.
  18. * //
  19. * return new Ext.Promise(function (resolve, reject) {
  20. * Ext.Ajax.request({
  21. * url: url,
  22. *
  23. * success: function (response) {
  24. * // Use the provided "resolve" method to deliver the result.
  25. * //
  26. * resolve(response.responseText);
  27. * },
  28. *
  29. * failure: function (response) {
  30. * // Use the provided "reject" method to deliver error message.
  31. * //
  32. * reject(response.status);
  33. * }
  34. * });
  35. * });
  36. * }
  37. *
  38. * getAjax('http://stuff').then(function (content) {
  39. * // content is responseText of ajax response
  40. * });
  41. *
  42. * To adapt the Ext JS `{@link Ext.data.Store store}` to use a Promise, you might do
  43. * something like this:
  44. *
  45. * loadCompanies: function() {
  46. * var companyStore = this.companyStore;
  47. *
  48. * return new Ext.Promise(function (resolve, reject) {
  49. * companyStore.load({
  50. * callback: function(records, operation, success) {
  51. * if (success) {
  52. * // Use the provided "resolve" method to drive the promise:
  53. * resolve(records);
  54. * }
  55. * else {
  56. * // Use the provided "reject" method to drive the promise:
  57. * reject("Error loading Companies.");
  58. * }
  59. * }
  60. * });
  61. * });
  62. * }
  63. *
  64. * @since 6.0.0
  65. */
  66. Ext.define('Ext.Promise', function () {
  67. var Polyfiller;
  68. return {
  69. requires: [
  70. 'Ext.promise.Promise'
  71. ],
  72. statics: {
  73. _ready: function () {
  74. // We can cache this now that our requires are met
  75. Polyfiller = Ext.promise.Promise;
  76. },
  77. /**
  78. * Returns a new Promise that will only resolve once all the specified
  79. * `promisesOrValues` have resolved.
  80. *
  81. * The resolution value will be an Array containing the resolution value of each
  82. * of the `promisesOrValues`.
  83. *
  84. * @param {Mixed[]/Ext.Promise[]/Ext.Promise} promisesOrValues An Array of values
  85. * or Promises, or a Promise of an Array of values or Promises.
  86. *
  87. * @return {Ext.Promise} A Promise of an Array of the resolved values.
  88. * @static
  89. */
  90. all: function () {
  91. return Polyfiller.all.apply(Polyfiller, arguments);
  92. },
  93. /**
  94. * Returns a promise that resolves or rejects as soon as one of the promises in the array resolves
  95. * or rejects, with the value or reason from that promise.
  96. * @param {Ext.promise.Promise[]} promises The promises.
  97. * @return {Ext.promise.Promise} The promise to be resolved when the race completes.
  98. *
  99. * @static
  100. * @since 6.5.0
  101. */
  102. race: function () {
  103. return Polyfiller.race.apply(Polyfiller, arguments);
  104. },
  105. /**
  106. * Convenience method that returns a new Promise rejected with the specified
  107. * reason.
  108. *
  109. * @param {Error} reason Rejection reason.
  110. * @return {Ext.Promise} The rejected Promise.
  111. * @static
  112. */
  113. reject: function (reason) {
  114. var deferred = new Ext.promise.Deferred();
  115. deferred.reject(reason);
  116. return deferred.promise;
  117. },
  118. /**
  119. * Returns a new Promise that either
  120. *
  121. * * Resolves immediately for the specified value, or
  122. * * Resolves or rejects when the specified promise (or third-party Promise or
  123. * then()-able) is resolved or rejected.
  124. *
  125. * @param {Mixed} value A Promise (or third-party Promise or then()-able)
  126. * or value.
  127. * @return {Ext.Promise} A Promise of the specified Promise or value.
  128. * @static
  129. */
  130. resolve: function (value) {
  131. var deferred = new Ext.promise.Deferred();
  132. deferred.resolve(value);
  133. return deferred.promise;
  134. }
  135. },
  136. constructor: function (action) {
  137. var deferred = new Ext.promise.Deferred();
  138. action(deferred.resolve.bind(deferred), deferred.reject.bind(deferred));
  139. return deferred.promise;
  140. }
  141. };},
  142. function (ExtPromise) {
  143. var P = Ext.global.Promise;
  144. if (P && P.resolve && !Ext.useExtPromises) {
  145. Ext.Promise = P;
  146. }
  147. else {
  148. ExtPromise._ready();
  149. }
  150. });