Kernel-Promises.st 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 localReturn = 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) localReturn = ex;
  41. else reject(ex);
  42. }
  43. });
  44. if (localReturn) throw localReturn; 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. self value: (self try: aBlock)
  66. !
  67. try: aBlock
  68. <inlineJS: '
  69. try {
  70. return aBlock._value();
  71. } catch(error) {
  72. // pass non-local returns undetected
  73. if (Array.isArray(error) && error.length === 1) throw error;
  74. self._signal_(error);
  75. }
  76. '>
  77. ! !
  78. !PromiseExecution methodsFor: 'settling'!
  79. signal: anErrorObject
  80. rejectBlock value: anErrorObject
  81. !
  82. value: anObject
  83. resolveBlock value: anObject
  84. ! !
  85. !PromiseExecution class methodsFor: 'instance creation'!
  86. resolveBlock: aBlock rejectBlock: anotherBlock
  87. ^ super new
  88. resolveBlock: aBlock rejectBlock: anotherBlock;
  89. yourself
  90. ! !
  91. Trait named: #TPromiseModel
  92. package: 'Kernel-Promises'!
  93. !TPromiseModel methodsFor: 'settling'!
  94. signal
  95. ^ self signal: Error new
  96. !
  97. signal: anErrorObject
  98. self subclassResponsibility
  99. !
  100. value
  101. ^ self value: nil
  102. !
  103. value: anObject
  104. self subclassResponsibility
  105. ! !
  106. Trait named: #TThenable
  107. package: 'Kernel-Promises'!
  108. !TThenable methodsFor: 'promises'!
  109. catch: aBlock
  110. <inlineJS: 'return self.then(null, function (err) { return aBlock._value_(err); })'>
  111. !
  112. on: aClass do: aBlock
  113. <inlineJS: 'return self.then(null, function (err) {
  114. if (err._isKindOf_(aClass)) return aBlock._value_(err);
  115. else throw err;
  116. })'>
  117. !
  118. on: aClass do: aBlock catch: anotherBlock
  119. ^ (self on: aClass do: aBlock) catch: anotherBlock
  120. !
  121. then: aBlockOrArray
  122. "Accepts a block or array of blocks.
  123. Each of blocks in the array or the singleton one is
  124. used in .then call to a promise, to accept a result
  125. and transform it to the result for the next one.
  126. In case a block has more than one argument
  127. and result is an array, first n-1 elements of the array
  128. are put into additional arguments beyond the first.
  129. The first argument always contains the result as-is."
  130. <inlineJS: '
  131. var array = Array.isArray(aBlockOrArray) ? aBlockOrArray : [aBlockOrArray];
  132. return array.reduce(function (soFar, aBlock) {
  133. return soFar.then(typeof aBlock === "function" && aBlock.length > 1 ?
  134. function (result) {
  135. if (Array.isArray(result)) {
  136. return aBlock._valueWithPossibleArguments_([result].concat(result.slice(0, aBlock.length-1)));
  137. } else {
  138. return aBlock._value_(result);
  139. }
  140. } :
  141. function (result) {
  142. return aBlock._value_(result);
  143. }
  144. );
  145. }, self)'>
  146. !
  147. then: aBlockOrArray catch: anotherBlock
  148. ^ (self then: aBlockOrArray) catch: anotherBlock
  149. !
  150. then: aBlockOrArray on: aClass do: aBlock
  151. ^ (self then: aBlockOrArray) on: aClass do: aBlock
  152. !
  153. then: aBlockOrArray on: aClass do: aBlock catch: anotherBlock
  154. ^ ((self then: aBlockOrArray) on: aClass do: aBlock) catch: anotherBlock
  155. ! !
  156. !TThenable methodsFor: 'testing'!
  157. isThenable
  158. ^ true
  159. ! !
  160. Promise setTraitComposition: {TThenable} asTraitComposition!
  161. Promise class setTraitComposition: {TPromiseModel} asTraitComposition!
  162. PromiseExecution setTraitComposition: {TPromiseModel} asTraitComposition!
  163. ! !