Browse Source

then: accepts a block or array of blocks

Herbert Vojčík 8 years ago
parent
commit
ab515cfbbb
2 changed files with 19 additions and 11 deletions
  1. 11 7
      src/Kernel-Promises.js
  2. 8 4
      src/Kernel-Promises.st

+ 11 - 7
src/Kernel-Promises.js

@@ -118,22 +118,26 @@ $core.addMethod(
 $core.method({
 selector: "then:",
 protocol: 'promises',
-fn: function (aBlock){
+fn: function (aBlockOrArray){
 var self=this;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
 return $core.withContext(function($ctx1) {
 //>>excludeEnd("ctx");
-return self.then(function (result) {
-    return aBlock._value_(result);
-});
+
+var array = Array.isArray(aBlockOrArray) ? aBlockOrArray : [aBlockOrArray];
+return array.reduce(function (soFar, aBlock) {
+    return soFar.then(function (result) {
+        return aBlock._value_(result);
+    });
+}, self);
 return self;
 //>>excludeStart("ctx", pragmas.excludeDebugContexts);
-}, function($ctx1) {$ctx1.fill(self,"then:",{aBlock:aBlock},$globals.Thenable)});
+}, function($ctx1) {$ctx1.fill(self,"then:",{aBlockOrArray:aBlockOrArray},$globals.Thenable)});
 //>>excludeEnd("ctx");
 },
 //>>excludeStart("ide", pragmas.excludeIdeData);
-args: ["aBlock"],
-source: "then: aBlock\x0a<return self.then(function (result) {\x0a    return aBlock._value_(result);\x0a})>",
+args: ["aBlockOrArray"],
+source: "then: aBlockOrArray\x0a<\x0avar array = Array.isArray(aBlockOrArray) ? aBlockOrArray : [aBlockOrArray];\x0areturn array.reduce(function (soFar, aBlock) {\x0a    return soFar.then(function (result) {\x0a        return aBlock._value_(result);\x0a    });\x0a}, self)>",
 referencedClasses: [],
 //>>excludeEnd("ide");
 messageSends: []

+ 8 - 4
src/Kernel-Promises.st

@@ -37,10 +37,14 @@ on: aClass do: aBlock catch: anotherBlock
 })>
 !
 
-then: aBlock
-<return self.then(function (result) {
-    return aBlock._value_(result);
-})>
+then: aBlockOrArray
+<
+var array = Array.isArray(aBlockOrArray) ? aBlockOrArray : [aBlockOrArray];
+return array.reduce(function (soFar, aBlock) {
+    return soFar.then(function (result) {
+        return aBlock._value_(result);
+    });
+}, self)>
 ! !
 
 Thenable subclass: #Promise