diff --git a/Promise.js b/Promise.js index 4ce46e9..e8dad6e 100644 --- a/Promise.js +++ b/Promise.js @@ -795,60 +795,53 @@ object.Constructor('ProxyPromise', Promise, { //--------------------------------------------------------------------- +// XXX EXPEREMENTAL... +// XXX DOCS... // XXX like promise but if a value can be generated sync then this will // run in sync otherwise it will fall back to being a promise... -// ...not sure where to return the sync value... // XXX potential problem is recursion (depth) on the sync stage... -// XXX not sure if we need this.. -var MaybePromice = -module.MaybePromice = +// XXX should we throw errors in sync mode??? +var MaybePromise = +module.MaybePromise = object.Constructor('MaybePromise', Promise, { - // XXX can we get into this without either .__error or .__result ??? + //error: undefined, + //value: undefined, + then: function(resolve, reject){ - if(this.hasOwnProperty('__error')){ + if(this.hasOwnProperty('error')){ return this.constructor.reject( reject ? - reject(this.__error) - : this.__error) } - if(this.hasOwnProperty('__result')){ + reject(this.error) + : this.error) } + if(this.hasOwnProperty('value')){ return this.constructor.resolve( - resolve(this.__result)) } }, - //catch: function(func){ - //}, - //finally: function(func){ - //}, + resolve(this.value)) } }, - //__error: undefined, - //__result: undefined, __new__: function(context, func){ var result var resolve = function(res){ result = res } + var rejected var error var reject = function(err){ + rejected = true error = err } - + // call... try{ func(resolve, reject) }catch(err){ reject(err) } - - if(error){ - this.__error = error - // async... - } else if(result instanceof Promise){ + if(!error + && result instanceof Promise){ return result } - // sync... - this.__result = result - // XXX - var obj = Reflect.construct( - MaybePromise.__proto__, - [], - MaybePromise) - return obj - }, + var obj = Promise.resolve(result) + obj.__proto__ = this.prototype + obj.value = result + rejected + && (obj.error = error) + return obj }, }) @@ -860,8 +853,8 @@ object.Mixin('PromiseMixin', 'soft', { iter: IterablePromise, interactive: InteractivePromise, cooperative: CooperativePromise, - // XXX - //maybe: MaybePromise, + // XXX EXPEREMENTAL... + maybe: MaybePromise, // XXX need error support... awaitOrRun: function(data, func){ diff --git a/README.md b/README.md index 547ac6a..6a44264 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,9 @@ Library of JavaScript type extensions, types and utilities. - [Promise proxies](#promise-proxies) - [`.as(..)`](#promiseas) - [`.(..)`](#promise-proxymethod) + - [Sync/Async promise](#syncasync-promise) + - [`Promise.maybe(..)` / `promise.MaybePromice(..)`](#promisemaybe--promisemaybepromice) + - [`.value` / `.error`](#maybe-promisevalue--maybe-promiseerror) - [Promise utilities](#promise-utilities) - [`Promise.awaitOrRun(..)`](#promiseawaitorrun) - [Generator extensions and utilities](#generator-extensions-and-utilities) @@ -2001,6 +2004,18 @@ the main `` is resolved. +### Sync/async promise + + +#### `Promise.maybe(..)` / `promise.MaybePromice(..)` + +XXX + +#### `.value` / `.error` + +XXX + + ### Promise utilities #### `Promise.awaitOrRun(..)` diff --git a/package.json b/package.json index c97e912..a6a6fc6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-types", - "version": "6.22.2", + "version": "6.23.0", "description": "Generic JavaScript types and type extensions...", "main": "main.js", "scripts": {