Kernel-Promises.st 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. Smalltalk createPackage: 'Kernel-Promises'!
  2. Object subclass: #Thenable
  3. instanceVariableNames: ''
  4. package: 'Kernel-Promises'!
  5. !Thenable commentStamp!
  6. I am the abstract base class for Promises.
  7. My subclasses should wrap existing JS implementations.
  8. I contain methods that wrap Promises/A+ `.then` behaviour.!
  9. !Thenable methodsFor: 'promises'!
  10. all: nadicBlock
  11. <return self.then(function (array) {
  12. return nadicBlock._valueWithPossibleArguments_(array);
  13. })>
  14. !
  15. catch: aBlock
  16. <return self.then(null, function (err) {
  17. return aBlock._value_(err);
  18. })>
  19. !
  20. on: aClass do: aBlock
  21. <return self.then(null, function (err) {
  22. if (err._isKindOf_(aClass)) return aBlock._value_(err);
  23. else throw err;
  24. })>
  25. !
  26. on: aClass do: aBlock catch: anotherBlock
  27. <return self.then(null, function (err) {
  28. try { if (err._isKindOf_(aClass)) return aBlock._value_(err); } catch (e) { err = e; }
  29. return anotherBlock._value_(err);
  30. })>
  31. !
  32. then: aBlock
  33. <return self.then(function (result) {
  34. return aBlock._value_(result);
  35. })>
  36. ! !
  37. Thenable subclass: #Promise
  38. instanceVariableNames: ''
  39. package: 'Kernel-Promises'!
  40. !JSObjectProxy methodsFor: '*Kernel-Promises'!
  41. all: nadicBlock
  42. <var js = self["@jsObject"];
  43. if (typeof js.then === "function")
  44. return $globals.Thenable.fn.prototype._all_.call(js, nadicBlock);
  45. else
  46. return self._doesNotUnderstand_(
  47. $globals.Message._new()
  48. ._selector_("all:")
  49. ._arguments_([nadicBlock])
  50. )>
  51. !
  52. catch: aBlock
  53. <var js = self["@jsObject"];
  54. if (typeof js.then === "function")
  55. return $globals.Thenable.fn.prototype._catch_.call(js, aBlock);
  56. else
  57. return self._doesNotUnderstand_(
  58. $globals.Message._new()
  59. ._selector_("catch:")
  60. ._arguments_([aBlock])
  61. )>
  62. !
  63. on: aClass do: aBlock
  64. <var js = self["@jsObject"];
  65. if (typeof js.then === "function")
  66. return $globals.Thenable.fn.prototype._on_do_.call(js, aClass, aBlock);
  67. else
  68. return self._doesNotUnderstand_(
  69. $globals.Message._new()
  70. ._selector_("on:do:")
  71. ._arguments_([aClass, aBlock])
  72. )>
  73. !
  74. on: aClass do: aBlock catch: anotherBlock
  75. <var js = self["@jsObject"];
  76. if (typeof js.then === "function")
  77. return $globals.Thenable.fn.prototype._on_do_catch_.call(js, aClass, aBlock, anotherBlock);
  78. else
  79. return self._doesNotUnderstand_(
  80. $globals.Message._new()
  81. ._selector_("on:do:catch:")
  82. ._arguments_([aClass, aBlock, anotherBlock])
  83. )>
  84. !
  85. then: aBlock
  86. <var js = self["@jsObject"];
  87. if (typeof js.then === "function")
  88. return $globals.Thenable.fn.prototype._then_.call(js, aBlock);
  89. else
  90. return self._doesNotUnderstand_(
  91. $globals.Message._new()
  92. ._selector_("then:")
  93. ._arguments_([aBlock])
  94. )>
  95. ! !