diff --git a/Promise.js b/Promise.js index 9716923..4aea458 100644 --- a/Promise.js +++ b/Promise.js @@ -118,8 +118,11 @@ object.Constructor('IterablePromise', Promise, { // // NOTE: .catch(..) and .finally(..) are implemented through .then(..) // so we do not need to overload those... + // NOTE: this is slightly different from .then(..) in that it can be + // called without arguments and return a promise wrapper. This can + // be useful to hide special promise functionality... then: function (onfulfilled, onrejected){ - return new Promise( + var p = new Promise( function(resolve, reject){ Promise.prototype.then.call(this, // NOTE: resolve(..) / reject(..) return undefined so @@ -130,7 +133,9 @@ object.Constructor('IterablePromise', Promise, { function(res){ reject(res) return res }) }.bind(this)) - .then(...arguments) }, + return arguments.length > 0 ? + p.then(...arguments) + : p }, // diff --git a/README.md b/README.md index 84bcd2d..17e6923 100644 --- a/README.md +++ b/README.md @@ -58,10 +58,12 @@ A library of JavaScript type extensions, types and type utilities. - [Interactive promises](#interactive-promises) - [`Promise.interactive(..)`](#promiseinteractive) - [`.send(..)`](#promise-intersend) + - [`.then(..)`](#promise-interthen) - [Cooperative promises](#cooperative-promises) - - [`Promise.cooperative(..)`](#promisecooperative) + - [`Promise.cooperative()`](#promisecooperative) - [`.set(..)`](#promise-coopset) - [`.isSet`](#promise-coopisset) + - [`.then(..)`](#promise-coopthen) - [Promise iteration](#promise-iteration) - [`Promise.iter(..)` / `promise.IterablePromise(..)`](#promiseiter--promiseiterablepromise) - [`.map(..)` / `.filter(..)` / `.reduce(..)`](#promise-itermap--promise-iterfilter--promise-iterreduce) @@ -1232,16 +1234,45 @@ Sending a message triggers message handlers registered via `(..)` passing each handler the arguments. +#### `.then(..)` + + + +See [`.then(..)`](#promise-iterthen--promise-itercatch--promise-iterfinally) for details. + + ### Cooperative promises -A _cooperative promise_ is one the state of which can be controlled -externally/cooperatively. +A _cooperative promise_ is one that can be finalized externally/cooperatively. - +This can be useful when breaking recursive dependencies between promises or when +it is simpler to thread the result receiver promise down the stack than building +a promise stack and manually threading the result up. + + +```javascript +``` + +Note that functionally this can be considered a special-case of an +[interactive promise](#interactive-promises), but in reality they are two +different implementations, the main differences are: +- _Cooperative promise_ constructor does not need a resolver function, +- _Cooperative promises_ do not the implement `.send(..)` API. + +Note that implementing _Cooperative promises_ on top of _Interactive promises_ +cleanly, though feeling more _"beautiful"_, would be more complex than the +current standalone implementation, as it would require both implementing +the `.set(..)` API/logic _and_ active encapsulation of the message API. -#### `Promise.cooperative(..)` + +#### `Promise.cooperative()` + +```bnf +Promise.cooperative() + -> +``` @@ -1256,6 +1287,13 @@ externally/cooperatively. +#### `.then(..)` + + + +See [`.then(..)`](#promise-iterthen--promise-itercatch--promise-iterfinally) for details. + + ### Promise iteration @@ -1338,6 +1376,10 @@ Promise.iter() +Note that `.then(..)` here can be called without arguments returning a generic +promise wrapper. This can be useful to hide the extended promise API from further +code. + #### Advanced handler diff --git a/package.json b/package.json index 1a4e9ae..8aebfb7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-types", - "version": "6.0.13", + "version": "6.0.14", "description": "Generic JavaScript types and type extensions...", "main": "main.js", "scripts": {