Smalltalk createPackage: 'Kernel-Promises'! Object subclass: #Thenable instanceVariableNames: '' package: 'Kernel-Promises'! !Thenable commentStamp! I am the abstract base class for Promises. My subclasses should wrap existing JS implementations. I contain methods that wrap Promises/A+ `.then` behaviour.! !Thenable methodsFor: 'promises'! catch: aBlock ! on: aClass do: aBlock ! on: aClass do: aBlock catch: anotherBlock ! then: aBlockOrArray "Accepts a block or array of blocks. Each of blocks in the array or the singleton one is used in .then call to a promise, to accept a result and transform it to the result for the next one. In case a block has more than one argument and result is an array, first n-1 elements of the array are put into additional arguments beyond the first. The first argument always contains the result as-is." < var array = Array.isArray(aBlockOrArray) ? aBlockOrArray : [aBlockOrArray]; return array.reduce(function (soFar, aBlock) { return soFar.then(typeof aBlock === "function" && aBlock.length >> 1 ? function (result) { if (Array.isArray(result)) { return aBlock._valueWithPossibleArguments_([result].concat(result.slice(0, aBlock.length-1))); } else { return aBlock._value_(result); } } : function (result) { return aBlock._value_(result); } ); }, self)> ! then: aBlockOrArray catch: anotherBlock ^ (self then: aBlockOrArray) catch: anotherBlock ! then: aBlockOrArray on: aClass do: aBlock ^ (self then: aBlockOrArray) on: aClass do: aBlock ! then: aBlockOrArray on: aClass do: aBlock catch: anotherBlock ^ ((self then: aBlockOrArray) on: aClass do: aBlock) catch: anotherBlock ! ! Thenable subclass: #Promise instanceVariableNames: '' package: 'Kernel-Promises'! !JSObjectProxy methodsFor: '*Kernel-Promises'! catch: aBlock ! on: aClass do: aBlock ! on: aClass do: aBlock catch: anotherBlock ! then: aBlockOrArray ! then: aBlockOrArray catch: anotherBlock ! then: aBlockOrArray on: aClass do: aBlock ! then: aBlockOrArray on: aClass do: aBlock catch: anotherBlock ! !