Kernel-Promises.st 3.6 KB

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