Kernel-Promises.st 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. Smalltalk createPackage: 'Kernel-Promises'!
  2. Object subclass: #Thenable
  3. instanceVariableNames: ''
  4. package: 'Kernel-Promises'!
  5. !Thenable commentStamp!
  6. I am the abstract base class for Promises.
  7. My subclasses should wrap existing JS implementations.
  8. I contain methods that wrap Promises/A+ `.then` behaviour.!
  9. !Thenable methodsFor: 'promises'!
  10. catch: aBlock
  11. <return self.then(null, function (err) {return $core.seamless(function () {
  12. return aBlock._value_(err);
  13. })})>
  14. !
  15. on: aClass do: aBlock
  16. <return self.then(null, function (err) {return $core.seamless(function () {
  17. if (err._isKindOf_(aClass)) return aBlock._value_(err);
  18. else throw err;
  19. })})>
  20. !
  21. on: aClass do: aBlock catch: anotherBlock
  22. <return self.then(null, function (err) {return $core.seamless(function () {
  23. try { if (err._isKindOf_(aClass)) return aBlock._value_(err); } catch (e) { err = e; }
  24. return anotherBlock._value_(err);
  25. })})>
  26. !
  27. then: aBlockOrArray
  28. "Accepts a block or array of blocks.
  29. Each of blocks in the array or the singleton one is
  30. used in .then call to a promise, to accept a result
  31. and transform it to the result for the next one.
  32. In case a block has more than one argument
  33. and result is an array, first n-1 elements of the array
  34. are put into additional arguments beyond the first.
  35. The first argument always contains the result as-is."
  36. <
  37. var array = Array.isArray(aBlockOrArray) ? aBlockOrArray : [aBlockOrArray];
  38. return array.reduce(function (soFar, aBlock) {
  39. return soFar.then(typeof aBlock === "function" && aBlock.length >> 1 ?
  40. function (result) {return $core.seamless(function () {
  41. if (Array.isArray(result)) {
  42. return aBlock._valueWithPossibleArguments_([result].concat(result.slice(0, aBlock.length-1)));
  43. } else {
  44. return aBlock._value_(result);
  45. }
  46. })} :
  47. function (result) {return $core.seamless(function () {
  48. return aBlock._value_(result);
  49. })}
  50. );
  51. }, self)>
  52. !
  53. then: aBlockOrArray catch: anotherBlock
  54. ^ (self then: aBlockOrArray) catch: anotherBlock
  55. !
  56. then: aBlockOrArray on: aClass do: aBlock
  57. ^ (self then: aBlockOrArray) on: aClass do: aBlock
  58. !
  59. then: aBlockOrArray on: aClass do: aBlock catch: anotherBlock
  60. ^ ((self then: aBlockOrArray) on: aClass do: aBlock) catch: anotherBlock
  61. ! !
  62. Thenable subclass: #Promise
  63. instanceVariableNames: ''
  64. package: 'Kernel-Promises'!
  65. !Promise class methodsFor: 'instance creation'!
  66. forBlock: aBlock
  67. "Returns a Promise that is resolved with the value of aBlock,
  68. and rejected if error happens while evaluating aBlock."
  69. <return Promise.resolve().then(function () {return $core.seamless(function () {return $recv(aBlock)._value_()})})>
  70. !
  71. new: aBlock
  72. "Returns a Promise that is eventually resolved or rejected.
  73. Pass a block that is called with one argument, model.
  74. You should call model value: ... to resolve the promise
  75. and model signal: ... to reject the promise.
  76. If error happens during run of the block,
  77. promise is rejected with that error as well."
  78. <return new Promise(function (resolve, reject) {
  79. var model = {value: resolve, signal: reject}; // TODO make faster
  80. aBlock._value_(model);
  81. })>
  82. !
  83. signal: anObject
  84. "Returns a Promise rejected with anObject."
  85. <return $recv(anObject)._in_(function (x) {return Promise.reject(x)})>
  86. !
  87. value: anObject
  88. "Returns a Promise resolved with anObject."
  89. <return $recv(anObject)._in_(function (x) {return Promise.resolve(x)})>
  90. ! !
  91. !JSObjectProxy methodsFor: '*Kernel-Promises'!
  92. catch: aBlock
  93. <var js = self["@jsObject"];
  94. if (typeof js.then === "function")
  95. return $globals.Thenable.fn.prototype._catch_.call(js, aBlock);
  96. else
  97. return self._doesNotUnderstand_(
  98. $globals.Message._new()
  99. ._selector_("catch:")
  100. ._arguments_([aBlock])
  101. )>
  102. !
  103. on: aClass do: aBlock
  104. <var js = self["@jsObject"];
  105. if (typeof js.then === "function")
  106. return $globals.Thenable.fn.prototype._on_do_.call(js, aClass, aBlock);
  107. else
  108. return self._doesNotUnderstand_(
  109. $globals.Message._new()
  110. ._selector_("on:do:")
  111. ._arguments_([aClass, aBlock])
  112. )>
  113. !
  114. on: aClass do: aBlock catch: anotherBlock
  115. <var js = self["@jsObject"];
  116. if (typeof js.then === "function")
  117. return $globals.Thenable.fn.prototype._on_do_catch_.call(js, aClass, aBlock, anotherBlock);
  118. else
  119. return self._doesNotUnderstand_(
  120. $globals.Message._new()
  121. ._selector_("on:do:catch:")
  122. ._arguments_([aClass, aBlock, anotherBlock])
  123. )>
  124. !
  125. then: aBlockOrArray
  126. <var js = self["@jsObject"];
  127. if (typeof js.then === "function")
  128. return $globals.Thenable.fn.prototype._then_.call(js, aBlockOrArray);
  129. else
  130. return self._doesNotUnderstand_(
  131. $globals.Message._new()
  132. ._selector_("then:")
  133. ._arguments_([aBlockOrArray])
  134. )>
  135. !
  136. then: aBlockOrArray catch: anotherBlock
  137. <var js = self["@jsObject"];
  138. if (typeof js.then === "function")
  139. return $globals.Thenable.fn.prototype._then_catch_.call(js, aBlockOrArray, anotherBlock);
  140. else
  141. return self._doesNotUnderstand_(
  142. $globals.Message._new()
  143. ._selector_("then:catch:")
  144. ._arguments_([aBlockOrArray, anotherBlock])
  145. )>
  146. !
  147. then: aBlockOrArray on: aClass do: aBlock
  148. <var js = self["@jsObject"];
  149. if (typeof js.then === "function")
  150. return $globals.Thenable.fn.prototype._then_on_do_.call(js, aBlockOrArray, aClass, aBlock);
  151. else
  152. return self._doesNotUnderstand_(
  153. $globals.Message._new()
  154. ._selector_("then:on:do:")
  155. ._arguments_([aBlockOrArray, aClass, aBlock])
  156. )>
  157. !
  158. then: aBlockOrArray on: aClass do: aBlock catch: anotherBlock
  159. <var js = self["@jsObject"];
  160. if (typeof js.then === "function")
  161. return $globals.Thenable.fn.prototype._then_on_do_catch_.call(js, aBlockOrArray, aClass, aBlock, anotherBlock);
  162. else
  163. return self._doesNotUnderstand_(
  164. $globals.Message._new()
  165. ._selector_("then:on:do:catch:")
  166. ._arguments_([aBlockOrArray, aClass, aBlock, anotherBlock])
  167. )>
  168. ! !