Kernel-Promises.st 3.5 KB

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