Kernel-Promises.st 4.8 KB

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