Browse Source

Add model signalIfFails: [ ... ] to Promise new:

You can do, for example

Promise new: [ :model |
   "possibly async, later:"
   model signalIfFails: [
     ... some sync code ...
     "async, even later:"
     model value: result ]
].

and the promise is resolved with result if sync code in block ran fine,
but is rejected when there is error during its execution.

Usable in async test, to do

  model signalIfFails: [
    ... assertions ...
  ]

Just a convenient shortcut for
  (Promise forBlock: [ ... ]) catch: [ :err | model signal: err ]
Herby Vojčík 3 years ago
parent
commit
888a80b451
2 changed files with 6 additions and 4 deletions
  1. 4 3
      lang/src/Kernel-Promises.js
  2. 2 1
      lang/src/Kernel-Promises.st

+ 4 - 3
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};\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($self._forBlock_(aBlock)); },\x0a\x09\x09signalIfFails: function (aBlock) { $self._forBlock_(aBlock)._catch_(reject); }\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};\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($self._forBlock_(aBlock)); },\x0a\x09\x09signalIfFails: function (aBlock) { $self._forBlock_(aBlock)._catch_(reject); }\x0a\x09};\x0a    aBlock._value_(model);\x0a})"]]],
 messageSends: []
 }, function ($methodClass){ return function (aBlock){
 var self=this,$self=this;
@@ -159,7 +159,8 @@ return new Promise(function (resolve, reject) {
     var model = {
 		value: resolve,
 		signal: reject,
-		do: function (aBlock) { resolve($self._forBlock_(aBlock)); }
+		do: function (aBlock) { resolve($self._forBlock_(aBlock)); },
+		signalIfFails: function (aBlock) { $self._forBlock_(aBlock)._catch_(reject); }
 	};
     aBlock._value_(model);
 });

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

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