Kernel-Promises.st 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 = {value: resolve, signal: 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. Trait named: #TThenable
  48. package: 'Kernel-Promises'!
  49. !TThenable methodsFor: 'promises'!
  50. catch: aBlock
  51. <inlineJS: 'return self.then(null, function (err) { return aBlock._value_(err); })'>
  52. !
  53. on: aClass do: aBlock
  54. <inlineJS: 'return self.then(null, function (err) {
  55. if (err._isKindOf_(aClass)) return aBlock._value_(err);
  56. else throw err;
  57. })'>
  58. !
  59. on: aClass do: aBlock catch: anotherBlock
  60. ^ (self on: aClass do: aBlock) catch: anotherBlock
  61. !
  62. then: aBlockOrArray
  63. "Accepts a block or array of blocks.
  64. Each of blocks in the array or the singleton one is
  65. used in .then call to a promise, to accept a result
  66. and transform it to the result for the next one.
  67. In case a block has more than one argument
  68. and result is an array, first n-1 elements of the array
  69. are put into additional arguments beyond the first.
  70. The first argument always contains the result as-is."
  71. <inlineJS: '
  72. var array = Array.isArray(aBlockOrArray) ? aBlockOrArray : [aBlockOrArray];
  73. return array.reduce(function (soFar, aBlock) {
  74. return soFar.then(typeof aBlock === "function" && aBlock.length > 1 ?
  75. function (result) {
  76. if (Array.isArray(result)) {
  77. return aBlock._valueWithPossibleArguments_([result].concat(result.slice(0, aBlock.length-1)));
  78. } else {
  79. return aBlock._value_(result);
  80. }
  81. } :
  82. function (result) {
  83. return aBlock._value_(result);
  84. }
  85. );
  86. }, self)'>
  87. !
  88. then: aBlockOrArray catch: anotherBlock
  89. ^ (self then: aBlockOrArray) catch: anotherBlock
  90. !
  91. then: aBlockOrArray on: aClass do: aBlock
  92. ^ (self then: aBlockOrArray) on: aClass do: aBlock
  93. !
  94. then: aBlockOrArray on: aClass do: aBlock catch: anotherBlock
  95. ^ ((self then: aBlockOrArray) on: aClass do: aBlock) catch: anotherBlock
  96. ! !
  97. !TThenable methodsFor: 'testing'!
  98. isThenable
  99. ^ true
  100. ! !
  101. Promise setTraitComposition: {TThenable} asTraitComposition!
  102. ! !