Kernel-Promises.st 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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: 'composites'!
  66. all: aCollection
  67. "Returns a Promise resolved with results of sub-promises."
  68. <return Promise.all($recv(aCollection)._asArray())>
  69. !
  70. any: aCollection
  71. "Returns a Promise resolved with first result of sub-promises."
  72. <return Promise.race($recv(aCollection)._asArray())>
  73. ! !
  74. !Promise class methodsFor: 'instance creation'!
  75. forBlock: aBlock
  76. "Returns a Promise that is resolved with the value of aBlock,
  77. and rejected if error happens while evaluating aBlock."
  78. ^ self new then: aBlock
  79. !
  80. new
  81. "Returns a dumb Promise resolved with nil."
  82. <return Promise.resolve()>
  83. !
  84. new: aBlock
  85. "Returns a Promise that is eventually resolved or rejected.
  86. Pass a block that is called with one argument, model.
  87. You should call model value: ... to resolve the promise
  88. and model signal: ... to reject the promise.
  89. If error happens during run of the block,
  90. promise is rejected with that error as well."
  91. <return new Promise(function (resolve, reject) {
  92. var model = {value: resolve, signal: reject}; // TODO make faster
  93. aBlock._value_(model);
  94. })>
  95. !
  96. signal: anObject
  97. "Returns a Promise rejected with anObject."
  98. <return $recv(anObject)._in_(function (x) {return Promise.reject(x)})>
  99. !
  100. value: anObject
  101. "Returns a Promise resolved with anObject."
  102. <return $recv(anObject)._in_(function (x) {return Promise.resolve(x)})>
  103. ! !
  104. !JSObjectProxy methodsFor: '*Kernel-Promises'!
  105. catch: aBlock
  106. <var js = self["@jsObject"];
  107. if (typeof js.then === "function")
  108. return $globals.Thenable.fn.prototype._catch_.call(js, aBlock);
  109. else
  110. return self._doesNotUnderstand_(
  111. $globals.Message._new()
  112. ._selector_("catch:")
  113. ._arguments_([aBlock])
  114. )>
  115. !
  116. on: aClass do: aBlock
  117. <var js = self["@jsObject"];
  118. if (typeof js.then === "function")
  119. return $globals.Thenable.fn.prototype._on_do_.call(js, aClass, aBlock);
  120. else
  121. return self._doesNotUnderstand_(
  122. $globals.Message._new()
  123. ._selector_("on:do:")
  124. ._arguments_([aClass, aBlock])
  125. )>
  126. !
  127. on: aClass do: aBlock catch: anotherBlock
  128. <var js = self["@jsObject"];
  129. if (typeof js.then === "function")
  130. return $globals.Thenable.fn.prototype._on_do_catch_.call(js, aClass, aBlock, anotherBlock);
  131. else
  132. return self._doesNotUnderstand_(
  133. $globals.Message._new()
  134. ._selector_("on:do:catch:")
  135. ._arguments_([aClass, aBlock, anotherBlock])
  136. )>
  137. !
  138. then: aBlockOrArray
  139. <var js = self["@jsObject"];
  140. if (typeof js.then === "function")
  141. return $globals.Thenable.fn.prototype._then_.call(js, aBlockOrArray);
  142. else
  143. return self._doesNotUnderstand_(
  144. $globals.Message._new()
  145. ._selector_("then:")
  146. ._arguments_([aBlockOrArray])
  147. )>
  148. !
  149. then: aBlockOrArray catch: anotherBlock
  150. <var js = self["@jsObject"];
  151. if (typeof js.then === "function")
  152. return $globals.Thenable.fn.prototype._then_catch_.call(js, aBlockOrArray, anotherBlock);
  153. else
  154. return self._doesNotUnderstand_(
  155. $globals.Message._new()
  156. ._selector_("then:catch:")
  157. ._arguments_([aBlockOrArray, anotherBlock])
  158. )>
  159. !
  160. then: aBlockOrArray on: aClass do: aBlock
  161. <var js = self["@jsObject"];
  162. if (typeof js.then === "function")
  163. return $globals.Thenable.fn.prototype._then_on_do_.call(js, aBlockOrArray, aClass, aBlock);
  164. else
  165. return self._doesNotUnderstand_(
  166. $globals.Message._new()
  167. ._selector_("then:on:do:")
  168. ._arguments_([aBlockOrArray, aClass, aBlock])
  169. )>
  170. !
  171. then: aBlockOrArray on: aClass do: aBlock catch: anotherBlock
  172. <var js = self["@jsObject"];
  173. if (typeof js.then === "function")
  174. return $globals.Thenable.fn.prototype._then_on_do_catch_.call(js, aBlockOrArray, aClass, aBlock, anotherBlock);
  175. else
  176. return self._doesNotUnderstand_(
  177. $globals.Message._new()
  178. ._selector_("then:on:do:catch:")
  179. ._arguments_([aBlockOrArray, aClass, aBlock, anotherBlock])
  180. )>
  181. ! !