API Docs for: 3.13.0
Show:

File: promise/js/promise.js

  1. /**
  2. Wraps the execution of asynchronous operations, providing a promise object that
  3. can be used to subscribe to the various ways the operation may terminate.
  4.  
  5. When the operation completes successfully, call the Resolver's `fulfill()`
  6. method, passing any relevant response data for subscribers. If the operation
  7. encounters an error or is unsuccessful in some way, call `reject()`, again
  8. passing any relevant data for subscribers.
  9.  
  10. The Resolver object should be shared only with the code resposible for
  11. resolving or rejecting it. Public access for the Resolver is through its
  12. _promise_, which is returned from the Resolver's `promise` property. While both
  13. Resolver and promise allow subscriptions to the Resolver's state changes, the
  14. promise may be exposed to non-controlling code. It is the preferable interface
  15. for adding subscriptions.
  16.  
  17. Subscribe to state changes in the Resolver with the promise's
  18. `then(callback, errback)` method. `then()` wraps the passed callbacks in a
  19. new Resolver and returns the corresponding promise, allowing chaining of
  20. asynchronous or synchronous operations. E.g.
  21. `promise.then(someAsyncFunc).then(anotherAsyncFunc)`
  22.  
  23. @module promise
  24. @since 3.9.0
  25. **/
  26.  
  27. /**
  28. A promise represents a value that may not yet be available. Promises allow
  29. you to chain asynchronous operations, write synchronous looking code and
  30. handle errors throughout the process.
  31.  
  32. This constructor takes a function as a parameter where you can insert the logic
  33. that fulfills or rejects this promise. The fulfillment value and the rejection
  34. reason can be any JavaScript value. It's encouraged that rejection reasons be
  35. error objects
  36.  
  37. <pre><code>
  38. var fulfilled = new Y.Promise(function (fulfill) {
  39. fulfill('I am a fulfilled promise');
  40. });
  41.  
  42. var rejected = new Y.Promise(function (fulfill, reject) {
  43. reject(new Error('I am a rejected promise'));
  44. });
  45. </code></pre>
  46.  
  47. @class Promise
  48. @constructor
  49. @param {Function} fn A function where to insert the logic that resolves this
  50. promise. Receives `fulfill` and `reject` functions as parameters.
  51. This function is called synchronously.
  52. **/
  53. function Promise(fn) {
  54. if (!(this instanceof Promise)) {
  55. return new Promise(fn);
  56. }
  57.  
  58. var resolver = new Promise.Resolver(this);
  59.  
  60. /**
  61. A reference to the resolver object that handles this promise
  62.  
  63. @property _resolver
  64. @type Object
  65. @private
  66. */
  67. this._resolver = resolver;
  68.  
  69. fn.call(this, function (value) {
  70. resolver.fulfill(value);
  71. }, function (reason) {
  72. resolver.reject(reason);
  73. });
  74. }
  75.  
  76. Y.mix(Promise.prototype, {
  77. /**
  78. Schedule execution of a callback to either or both of "fulfill" and
  79. "reject" resolutions for this promise. The callbacks are wrapped in a new
  80. promise and that promise is returned. This allows operation chaining ala
  81. `functionA().then(functionB).then(functionC)` where `functionA` returns
  82. a promise, and `functionB` and `functionC` _may_ return promises.
  83.  
  84. Asynchronicity of the callbacks is guaranteed.
  85.  
  86. @method then
  87. @param {Function} [callback] function to execute if the promise
  88. resolves successfully
  89. @param {Function} [errback] function to execute if the promise
  90. resolves unsuccessfully
  91. @return {Promise} A promise wrapping the resolution of either "resolve" or
  92. "reject" callback
  93. **/
  94. then: function (callback, errback) {
  95. return this._resolver.then(callback, errback);
  96. },
  97.  
  98. /**
  99. Returns the current status of the operation. Possible results are
  100. "pending", "fulfilled", and "rejected".
  101.  
  102. @method getStatus
  103. @return {String}
  104. **/
  105. getStatus: function () {
  106. return this._resolver.getStatus();
  107. }
  108. });
  109.  
  110. /**
  111. Checks if an object or value is a promise. This is cross-implementation
  112. compatible, so promises returned from other libraries or native components
  113. that are compatible with the Promises A+ spec should be recognized by this
  114. method.
  115.  
  116. @method isPromise
  117. @param {Any} obj The object to test
  118. @return {Boolean} Whether the object is a promise or not
  119. @static
  120. **/
  121. Promise.isPromise = function (obj) {
  122. // We test promises by form to be able to identify other implementations
  123. // as promises. This is important for cross compatibility and in particular
  124. // Y.when which should take any kind of promise
  125. return !!obj && typeof obj.then === 'function';
  126. };
  127.  
  128. Y.Promise = Promise;
  129.