Browse Source

Rework of new "Promise new:" helpers; are sync.

(I know it is technically a breaking change,
but them being async was out there for just a few hours)

Examples to clarify:

(Promise new: [ :model | model do: [ #bar ]; value: #baz ])
  then: #inspect catch: #inspect "Shows #bar via first inspect"

(Promise new: [ :model | model do: [ #bar foo ]; value: #baz ])
  then: #inspect catch: #inspect "Shows DNU via second inspect"

(Promise new: [ :model | model try: [ #bar ]; value: #baz ])
  then: #inspect catch: #inspect "Shows #baz via first inspect"

(Promise new: [ :model | model try: [ #bar foo ]; value: #baz ])
  then: #inspect catch: #inspect "Shows DNU via second inspect"
Herby Vojčík 3 years ago
parent
commit
b30786774e
3 changed files with 15 additions and 9 deletions
  1. 3 3
      CHANGELOG
  2. 7 4
      lang/src/Kernel-Promises.js
  3. 5 2
      lang/src/Kernel-Promises.st

+ 3 - 3
CHANGELOG

@@ -1,9 +1,9 @@
-7 Oct 2020 - Release 0.29.4
+7 Oct 2020 - Release 0.29.5
 ===================================
 
-* Convenient method for model passed to Promise new: block
+* Convenient methods for model passed to Promise new: block
 
-Commits: https://lolg.it/amber/amber/commits/0.29.4.
+Commits: https://lolg.it/amber/amber/commits/0.29.5.
 
 
 3 Oct 2020 - Release 0.29.3

+ 7 - 4
lang/src/Kernel-Promises.js

@@ -145,10 +145,10 @@ selector: "new:",
 protocol: "instance creation",
 //>>excludeStart("ide", pragmas.excludeIdeData);
 args: ["aBlock"],
-source: "new: aBlock\x0a\x22Returns a Promise that is eventually resolved or rejected.\x0aPass a block that is called with one argument, model.\x0aYou should call model value: ... to resolve the promise\x0aand model signal: ... to reject the promise.\x0aIf error happens during run of the block,\x0apromise is rejected with that error as well.\x22\x0a<inlineJS: 'return new Promise(function (resolve, reject) {\x0a    var model = {\x0a\x09\x09value: resolve,\x0a\x09\x09signal: reject,\x0a\x09\x09do: function (aBlock) { resolve($self._forBlock_(aBlock)); },\x0a\x09\x09signalIfFails: function (aBlock) { $self._forBlock_(aBlock)._catch_(reject); }\x0a\x09};\x0a    aBlock._value_(model);\x0a})'>",
+source: "new: aBlock\x0a\x22Returns a Promise that is eventually resolved or rejected.\x0aPass a block that is called with one argument, model.\x0aYou should call model value: ... to resolve the promise\x0aand model signal: ... to reject the promise.\x0aIf error happens during run of the block,\x0apromise is rejected with that error as well.\x22\x0a<inlineJS: 'return new Promise(function (resolve, reject) {\x0a    var model = {\x0a\x09\x09value: resolve,\x0a\x09\x09signal: reject,\x0a\x09\x09do: function (aBlock) { resolve(this.try(aBlock)); },\x0a\x09\x09try: function (aBlock) {\x0a\x09\x09\x09try { return aBlock._value(); }\x0a\x09\x09\x09catch (e) { reject(e); }\x0a\x09\x09}\x0a\x09};\x0a    aBlock._value_(model);\x0a})'>",
 referencedClasses: [],
 //>>excludeEnd("ide");
-pragmas: [["inlineJS:", ["return new Promise(function (resolve, reject) {\x0a    var model = {\x0a\x09\x09value: resolve,\x0a\x09\x09signal: reject,\x0a\x09\x09do: function (aBlock) { resolve($self._forBlock_(aBlock)); },\x0a\x09\x09signalIfFails: function (aBlock) { $self._forBlock_(aBlock)._catch_(reject); }\x0a\x09};\x0a    aBlock._value_(model);\x0a})"]]],
+pragmas: [["inlineJS:", ["return new Promise(function (resolve, reject) {\x0a    var model = {\x0a\x09\x09value: resolve,\x0a\x09\x09signal: reject,\x0a\x09\x09do: function (aBlock) { resolve(this.try(aBlock)); },\x0a\x09\x09try: function (aBlock) {\x0a\x09\x09\x09try { return aBlock._value(); }\x0a\x09\x09\x09catch (e) { reject(e); }\x0a\x09\x09}\x0a\x09};\x0a    aBlock._value_(model);\x0a})"]]],
 messageSends: []
 }, function ($methodClass){ return function (aBlock){
 var self=this,$self=this;
@@ -159,8 +159,11 @@ return new Promise(function (resolve, reject) {
     var model = {
 		value: resolve,
 		signal: reject,
-		do: function (aBlock) { resolve($self._forBlock_(aBlock)); },
-		signalIfFails: function (aBlock) { $self._forBlock_(aBlock)._catch_(reject); }
+		do: function (aBlock) { resolve(this.try(aBlock)); },
+		try: function (aBlock) {
+			try { return aBlock._value(); }
+			catch (e) { reject(e); }
+		}
 	};
     aBlock._value_(model);
 });

+ 5 - 2
lang/src/Kernel-Promises.st

@@ -43,8 +43,11 @@ promise is rejected with that error as well."
     var model = {
 		value: resolve,
 		signal: reject,
-		do: function (aBlock) { resolve($self._forBlock_(aBlock)); },
-		signalIfFails: function (aBlock) { $self._forBlock_(aBlock)._catch_(reject); }
+		do: function (aBlock) { resolve(this.try(aBlock)); },
+		try: function (aBlock) {
+			try { return aBlock._value(); }
+			catch (e) { reject(e); }
+		}
 	};
     aBlock._value_(model);
 })'>