Kernel-Promises.st 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. !JSObjectProxy methodsFor: '*Kernel-Promises'!
  66. catch: aBlock
  67. <var js = self["@jsObject"];
  68. if (typeof js.then === "function")
  69. return $globals.Thenable.fn.prototype._catch_.call(js, aBlock);
  70. else
  71. return self._doesNotUnderstand_(
  72. $globals.Message._new()
  73. ._selector_("catch:")
  74. ._arguments_([aBlock])
  75. )>
  76. !
  77. on: aClass do: aBlock
  78. <var js = self["@jsObject"];
  79. if (typeof js.then === "function")
  80. return $globals.Thenable.fn.prototype._on_do_.call(js, aClass, aBlock);
  81. else
  82. return self._doesNotUnderstand_(
  83. $globals.Message._new()
  84. ._selector_("on:do:")
  85. ._arguments_([aClass, aBlock])
  86. )>
  87. !
  88. on: aClass do: aBlock catch: anotherBlock
  89. <var js = self["@jsObject"];
  90. if (typeof js.then === "function")
  91. return $globals.Thenable.fn.prototype._on_do_catch_.call(js, aClass, aBlock, anotherBlock);
  92. else
  93. return self._doesNotUnderstand_(
  94. $globals.Message._new()
  95. ._selector_("on:do:catch:")
  96. ._arguments_([aClass, aBlock, anotherBlock])
  97. )>
  98. !
  99. then: aBlockOrArray
  100. <var js = self["@jsObject"];
  101. if (typeof js.then === "function")
  102. return $globals.Thenable.fn.prototype._then_.call(js, aBlockOrArray);
  103. else
  104. return self._doesNotUnderstand_(
  105. $globals.Message._new()
  106. ._selector_("then:")
  107. ._arguments_([aBlockOrArray])
  108. )>
  109. !
  110. then: aBlockOrArray catch: anotherBlock
  111. <var js = self["@jsObject"];
  112. if (typeof js.then === "function")
  113. return $globals.Thenable.fn.prototype._then_catch_.call(js, aBlockOrArray, anotherBlock);
  114. else
  115. return self._doesNotUnderstand_(
  116. $globals.Message._new()
  117. ._selector_("then:catch:")
  118. ._arguments_([aBlockOrArray, anotherBlock])
  119. )>
  120. !
  121. then: aBlockOrArray on: aClass do: aBlock
  122. <var js = self["@jsObject"];
  123. if (typeof js.then === "function")
  124. return $globals.Thenable.fn.prototype._then_on_do_.call(js, aBlockOrArray, aClass, aBlock);
  125. else
  126. return self._doesNotUnderstand_(
  127. $globals.Message._new()
  128. ._selector_("then:on:do:")
  129. ._arguments_([aBlockOrArray, aClass, aBlock])
  130. )>
  131. !
  132. then: aBlockOrArray on: aClass do: aBlock catch: anotherBlock
  133. <var js = self["@jsObject"];
  134. if (typeof js.then === "function")
  135. return $globals.Thenable.fn.prototype._then_on_do_catch_.call(js, aBlockOrArray, aClass, aBlock, anotherBlock);
  136. else
  137. return self._doesNotUnderstand_(
  138. $globals.Message._new()
  139. ._selector_("then:on:do:catch:")
  140. ._arguments_([aBlockOrArray, aClass, aBlock, anotherBlock])
  141. )>
  142. ! !