Kernel-Promises.st 4.6 KB

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