Kernel-Promises.st 3.4 KB

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