Kernel-Promises.st 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. Smalltalk createPackage: 'Kernel-Promises'!
  2. Object subclass: #Promise
  3. slots: {}
  4. package: 'Kernel-Promises'!
  5. !Promise class methodsFor: 'composites'!
  6. all: aCollection
  7. "Returns a Promise resolved with results of sub-promises."
  8. <inlineJS: 'return Promise.all($recv(aCollection)._asArray())'>
  9. !
  10. any: aCollection
  11. "Returns a Promise resolved with first result of sub-promises."
  12. <inlineJS: 'return Promise.race($recv(aCollection)._asArray())'>
  13. ! !
  14. !Promise class methodsFor: 'instance creation'!
  15. delayMilliseconds: aNumber
  16. ^ self new: [ :model | [ model value: nil ] valueWithTimeout: aNumber ]
  17. !
  18. forBlock: aBlock
  19. "Returns a Promise that is resolved with the value of aBlock,
  20. and rejected if error happens while evaluating aBlock."
  21. ^ self new then: aBlock
  22. !
  23. new
  24. "Returns a dumb Promise resolved with nil."
  25. <inlineJS: 'return Promise.resolve()'>
  26. !
  27. new: aBlock
  28. "Returns a Promise that is eventually resolved or rejected.
  29. Pass a block that is called with one argument, model.
  30. You should call model value: ... to resolve the promise
  31. and model signal: ... to reject the promise.
  32. If error happens during run of the block,
  33. promise is rejected with that error as well."
  34. <inlineJS: '
  35. var nonLocalReturn = null,
  36. promise = new Promise(function (resolve, reject) {
  37. var model = $globals.PromiseExecution._resolveBlock_rejectBlock_(resolve, reject);
  38. try { aBlock._value_(model); }
  39. catch (ex) {
  40. if (Array.isArray(ex) && ex.length === 1) nonLocalReturn = ex;
  41. else reject(ex);
  42. }
  43. });
  44. if (nonLocalReturn) throw nonLocalReturn; else return promise;
  45. '>
  46. !
  47. signal: anObject
  48. "Returns a Promise rejected with anObject."
  49. <inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.reject(x)})'>
  50. !
  51. value: anObject
  52. "Returns a Promise resolved with anObject."
  53. <inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.resolve(x)})'>
  54. ! !
  55. Object subclass: #PromiseExecution
  56. slots: {#resolveBlock. #rejectBlock}
  57. package: 'Kernel-Promises'!
  58. !PromiseExecution methodsFor: 'accessing'!
  59. resolveBlock: aBlock rejectBlock: anotherBlock
  60. resolveBlock := aBlock.
  61. rejectBlock := anotherBlock
  62. ! !
  63. !PromiseExecution methodsFor: 'evaluating'!
  64. do: aBlock
  65. "Executes a block 'in the context of a promise' and resolves.
  66. That is, if it ends with an error, promise is rejected.
  67. If a block succeeds, promise is resolved with its return value.
  68. Non-local returns are also treated as an error and reified as rejections."
  69. self value: (self try: aBlock)
  70. !
  71. try: aBlock
  72. "Executes a block 'in the context of a promise'.
  73. That is, if it ends with an error, promise is rejected.
  74. Non-local returns are also treated as an error and reified as rejections."
  75. <inlineJS: '
  76. try {
  77. return aBlock._value();
  78. } catch(error) {
  79. $self._signal_($globals.NonLifoReturn._reifyIfFeasible_(error));
  80. }
  81. '>
  82. ! !
  83. !PromiseExecution methodsFor: 'settling'!
  84. signal: anErrorObject
  85. rejectBlock value: anErrorObject
  86. !
  87. value: anObject
  88. resolveBlock value: anObject
  89. ! !
  90. !PromiseExecution class methodsFor: 'instance creation'!
  91. resolveBlock: aBlock rejectBlock: anotherBlock
  92. ^ super new
  93. resolveBlock: aBlock rejectBlock: anotherBlock;
  94. yourself
  95. ! !
  96. Trait named: #TPromiseModel
  97. package: 'Kernel-Promises'!
  98. !TPromiseModel methodsFor: 'settling'!
  99. signal
  100. ^ self signal: Error new
  101. !
  102. signal: anErrorObject
  103. self subclassResponsibility
  104. !
  105. value
  106. ^ self value: nil
  107. !
  108. value: anObject
  109. self subclassResponsibility
  110. ! !
  111. Trait named: #TThenable
  112. package: 'Kernel-Promises'!
  113. !TThenable methodsFor: 'promises'!
  114. catch: aBlock
  115. <inlineJS: 'return self.then(null, function (err) {
  116. return aBlock._value_($globals.NonLifoReturn._reifyIfFeasible_(err));
  117. })'>
  118. !
  119. on: aClass do: aBlock
  120. <inlineJS: 'return self.then(null, function (err) {
  121. var reified = $globals.NonLifoReturn._reifyIfFeasible_(err);
  122. if (reified._isKindOf_(aClass)) return aBlock._value_(reified);
  123. else throw err;
  124. })'>
  125. !
  126. on: aClass do: aBlock catch: anotherBlock
  127. ^ (self on: aClass do: aBlock) catch: anotherBlock
  128. !
  129. then: aBlockOrArray
  130. "Accepts a block or array of blocks.
  131. Each of blocks in the array or the singleton one is
  132. used in .then call to a promise, to accept a result
  133. and transform it to the result for the next one.
  134. In case a block has more than one argument
  135. and result is an array, first n-1 elements of the array
  136. are put into additional arguments beyond the first.
  137. The first argument always contains the result as-is."
  138. <inlineJS: '
  139. var array = Array.isArray(aBlockOrArray) ? aBlockOrArray : [aBlockOrArray];
  140. return array.reduce(function (soFar, aBlock) {
  141. return soFar.then(typeof aBlock === "function" && aBlock.length > 1 ?
  142. function (result) {
  143. if (Array.isArray(result)) {
  144. return aBlock._valueWithPossibleArguments_([result].concat(result.slice(0, aBlock.length-1)));
  145. } else {
  146. return aBlock._value_(result);
  147. }
  148. } :
  149. function (result) {
  150. return aBlock._value_(result);
  151. }
  152. );
  153. }, self)'>
  154. !
  155. then: aBlockOrArray catch: anotherBlock
  156. ^ (self then: aBlockOrArray) catch: anotherBlock
  157. !
  158. then: aBlockOrArray on: aClass do: aBlock
  159. ^ (self then: aBlockOrArray) on: aClass do: aBlock
  160. !
  161. then: aBlockOrArray on: aClass do: aBlock catch: anotherBlock
  162. ^ ((self then: aBlockOrArray) on: aClass do: aBlock) catch: anotherBlock
  163. ! !
  164. !TThenable methodsFor: 'testing'!
  165. isThenable
  166. ^ true
  167. ! !
  168. Promise setTraitComposition: {TThenable} asTraitComposition!
  169. Promise class setTraitComposition: {TPromiseModel} asTraitComposition!
  170. PromiseExecution setTraitComposition: {TPromiseModel} asTraitComposition!
  171. ! !