Promise.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. define([
  2. "../_base/lang"
  3. ], function(lang){
  4. "use strict";
  5. // module:
  6. // dojo/promise/Promise
  7. function throwAbstract(){
  8. throw new TypeError("abstract");
  9. }
  10. return lang.extend(function Promise(){
  11. // summary:
  12. // The public interface to a deferred.
  13. // description:
  14. // The public interface to a deferred. All promises in Dojo are
  15. // instances of this class.
  16. }, {
  17. then: function(callback, errback, progback){
  18. // summary:
  19. // Add new callbacks to the promise.
  20. // description:
  21. // Add new callbacks to the deferred. Callbacks can be added
  22. // before or after the deferred is fulfilled.
  23. // callback: Function?
  24. // Callback to be invoked when the promise is resolved.
  25. // Receives the resolution value.
  26. // errback: Function?
  27. // Callback to be invoked when the promise is rejected.
  28. // Receives the rejection error.
  29. // progback: Function?
  30. // Callback to be invoked when the promise emits a progress
  31. // update. Receives the progress update.
  32. // returns: dojo/promise/Promise
  33. // Returns a new promise for the result of the callback(s).
  34. // This can be used for chaining many asynchronous operations.
  35. throwAbstract();
  36. },
  37. cancel: function(reason, strict){
  38. // summary:
  39. // Inform the deferred it may cancel its asynchronous operation.
  40. // description:
  41. // Inform the deferred it may cancel its asynchronous operation.
  42. // The deferred's (optional) canceler is invoked and the
  43. // deferred will be left in a rejected state. Can affect other
  44. // promises that originate with the same deferred.
  45. // reason: any
  46. // A message that may be sent to the deferred's canceler,
  47. // explaining why it's being canceled.
  48. // strict: Boolean?
  49. // If strict, will throw an error if the deferred has already
  50. // been fulfilled and consequently cannot be canceled.
  51. // returns: any
  52. // Returns the rejection reason if the deferred was canceled
  53. // normally.
  54. throwAbstract();
  55. },
  56. isResolved: function(){
  57. // summary:
  58. // Checks whether the promise has been resolved.
  59. // returns: Boolean
  60. throwAbstract();
  61. },
  62. isRejected: function(){
  63. // summary:
  64. // Checks whether the promise has been rejected.
  65. // returns: Boolean
  66. throwAbstract();
  67. },
  68. isFulfilled: function(){
  69. // summary:
  70. // Checks whether the promise has been resolved or rejected.
  71. // returns: Boolean
  72. throwAbstract();
  73. },
  74. isCanceled: function(){
  75. // summary:
  76. // Checks whether the promise has been canceled.
  77. // returns: Boolean
  78. throwAbstract();
  79. },
  80. always: function(callbackOrErrback){
  81. // summary:
  82. // Add a callback to be invoked when the promise is resolved
  83. // or rejected.
  84. // callbackOrErrback: Function?
  85. // A function that is used both as a callback and errback.
  86. // returns: dojo/promise/Promise
  87. // Returns a new promise for the result of the callback/errback.
  88. return this.then(callbackOrErrback, callbackOrErrback);
  89. },
  90. otherwise: function(errback){
  91. // summary:
  92. // Add new errbacks to the promise.
  93. // errback: Function?
  94. // Callback to be invoked when the promise is rejected.
  95. // returns: dojo/promise/Promise
  96. // Returns a new promise for the result of the errback.
  97. return this.then(null, errback);
  98. },
  99. trace: function(){
  100. return this;
  101. },
  102. traceRejected: function(){
  103. return this;
  104. },
  105. toString: function(){
  106. // returns: string
  107. // Returns `[object Promise]`.
  108. return "[object Promise]";
  109. }
  110. });
  111. });