Kernel-Promises.st 3.6 KB

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