Kernel-Promises.st 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. !JSObjectProxy methodsFor: '*Kernel-Promises'!
  38. all: nadicBlock
  39. <var js = self["@jsObject"];
  40. if (typeof js.then === "function")
  41. return $globals.Thenable.fn.prototype._all_.call(js, nadicBlock);
  42. else
  43. return self._doesNotUnderstand_(
  44. $globals.Message._new()
  45. ._selector_("all:")
  46. ._arguments_([nadicBlock])
  47. )>
  48. !
  49. catch: aBlock
  50. <var js = self["@jsObject"];
  51. if (typeof js.then === "function")
  52. return $globals.Thenable.fn.prototype._catch_.call(js, aBlock);
  53. else
  54. return self._doesNotUnderstand_(
  55. $globals.Message._new()
  56. ._selector_("catch:")
  57. ._arguments_([aBlock])
  58. )>
  59. !
  60. on: aClass do: aBlock
  61. <var js = self["@jsObject"];
  62. if (typeof js.then === "function")
  63. return $globals.Thenable.fn.prototype._on_do_.call(js, aClass, aBlock);
  64. else
  65. return self._doesNotUnderstand_(
  66. $globals.Message._new()
  67. ._selector_("on:do:")
  68. ._arguments_([aClass, aBlock])
  69. )>
  70. !
  71. on: aClass do: aBlock catch: anotherBlock
  72. <var js = self["@jsObject"];
  73. if (typeof js.then === "function")
  74. return $globals.Thenable.fn.prototype._on_do_catch_.call(js, aClass, aBlock, anotherBlock);
  75. else
  76. return self._doesNotUnderstand_(
  77. $globals.Message._new()
  78. ._selector_("on:do:catch:")
  79. ._arguments_([aClass, aBlock, anotherBlock])
  80. )>
  81. !
  82. then: aBlock
  83. <var js = self["@jsObject"];
  84. if (typeof js.then === "function")
  85. return $globals.Thenable.fn.prototype._then_.call(js, aBlock);
  86. else
  87. return self._doesNotUnderstand_(
  88. $globals.Message._new()
  89. ._selector_("then:")
  90. ._arguments_([aBlock])
  91. )>
  92. ! !