Kernel-Promises.st 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. forBlock: aBlock
  16. "Returns a Promise that is resolved with the value of aBlock,
  17. and rejected if error happens while evaluating aBlock."
  18. ^ self new then: aBlock
  19. !
  20. new
  21. "Returns a dumb Promise resolved with nil."
  22. <inlineJS: 'return Promise.resolve()'>
  23. !
  24. new: aBlock
  25. "Returns a Promise that is eventually resolved or rejected.
  26. Pass a block that is called with one argument, model.
  27. You should call model value: ... to resolve the promise
  28. and model signal: ... to reject the promise.
  29. If error happens during run of the block,
  30. promise is rejected with that error as well."
  31. <inlineJS: 'return new Promise(function (resolve, reject) {
  32. var model = {value: resolve, signal: reject};
  33. aBlock._value_(model);
  34. })'>
  35. !
  36. signal: anObject
  37. "Returns a Promise rejected with anObject."
  38. <inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.reject(x)})'>
  39. !
  40. value: anObject
  41. "Returns a Promise resolved with anObject."
  42. <inlineJS: 'return $recv(anObject)._in_(function (x) {return Promise.resolve(x)})'>
  43. ! !
  44. Trait named: #TThenable
  45. package: 'Kernel-Promises'!
  46. !TThenable methodsFor: 'promises'!
  47. catch: aBlock
  48. <inlineJS: 'return self.then(null, function (err) { return aBlock._value_(err); })'>
  49. !
  50. on: aClass do: aBlock
  51. <inlineJS: 'return self.then(null, function (err) {
  52. if (err._isKindOf_(aClass)) return aBlock._value_(err);
  53. else throw err;
  54. })'>
  55. !
  56. on: aClass do: aBlock catch: anotherBlock
  57. ^ (self on: aClass do: aBlock) catch: anotherBlock
  58. !
  59. then: aBlockOrArray
  60. "Accepts a block or array of blocks.
  61. Each of blocks in the array or the singleton one is
  62. used in .then call to a promise, to accept a result
  63. and transform it to the result for the next one.
  64. In case a block has more than one argument
  65. and result is an array, first n-1 elements of the array
  66. are put into additional arguments beyond the first.
  67. The first argument always contains the result as-is."
  68. <inlineJS: '
  69. var array = Array.isArray(aBlockOrArray) ? aBlockOrArray : [aBlockOrArray];
  70. return array.reduce(function (soFar, aBlock) {
  71. return soFar.then(typeof aBlock === "function" && aBlock.length > 1 ?
  72. function (result) {
  73. if (Array.isArray(result)) {
  74. return aBlock._valueWithPossibleArguments_([result].concat(result.slice(0, aBlock.length-1)));
  75. } else {
  76. return aBlock._value_(result);
  77. }
  78. } :
  79. function (result) {
  80. return aBlock._value_(result);
  81. }
  82. );
  83. }, self)'>
  84. !
  85. then: aBlockOrArray catch: anotherBlock
  86. ^ (self then: aBlockOrArray) catch: anotherBlock
  87. !
  88. then: aBlockOrArray on: aClass do: aBlock
  89. ^ (self then: aBlockOrArray) on: aClass do: aBlock
  90. !
  91. then: aBlockOrArray on: aClass do: aBlock catch: anotherBlock
  92. ^ ((self then: aBlockOrArray) on: aClass do: aBlock) catch: anotherBlock
  93. ! !
  94. Promise setTraitComposition: {TThenable} asTraitComposition!
  95. ! !