From 881df1be213deef4ba6e687fc7a6f900ce4c6ce3 Mon Sep 17 00:00:00 2001 From: "Alex A. Naanou" Date: Sat, 3 Dec 2022 13:17:11 +0300 Subject: [PATCH] added error handling to .awaitOrRun(..)... Signed-off-by: Alex A. Naanou --- Promise.js | 88 +++++++++++++++++++++++++++++++++++++++++++++++++--- README.md | 7 +++-- package.json | 2 +- 3 files changed, 90 insertions(+), 7 deletions(-) diff --git a/Promise.js b/Promise.js index 357d711..670049e 100644 --- a/Promise.js +++ b/Promise.js @@ -793,6 +793,65 @@ object.Constructor('ProxyPromise', Promise, { +//--------------------------------------------------------------------- + +// 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 = +object.Constructor('MaybePromise', Promise, { + // XXX can we get into this without either .__error or .__result ??? + then: function(resolve, reject){ + if(this.hasOwnProperty('__error')){ + return this.constructor.reject( + reject ? + reject(this.__error) + : this.__error) } + if(this.hasOwnProperty('__result')){ + return this.constructor.resolve( + resolve(this.__result)) } }, + //catch: function(func){ + //}, + //finally: function(func){ + //}, + + //__error: undefined, + //__result: undefined, + __new__: function(context, func){ + var result + var resolve = function(res){ + result = res } + var error + var reject = function(err){ + error = err } + + try{ + func(resolve, reject) + }catch(err){ + reject(err) } + + if(error){ + this.__error = error + + // async... + } else if(result instanceof Promise){ + return result } + + // sync... + this.__result = result + // XXX + var obj = Reflect.construct( + MaybePromise.__proto__, + [], + MaybePromise) + return obj + }, +}) + + //--------------------------------------------------------------------- var PromiseMixin = @@ -801,10 +860,17 @@ object.Mixin('PromiseMixin', 'soft', { iter: IterablePromise, interactive: InteractivePromise, cooperative: CooperativePromise, - + // XXX + //maybe: MaybePromise, + + // XXX need error support... awaitOrRun: function(data, func){ data = [...arguments] func = data.pop() + var error + if(typeof(data.at(-1)) == 'function'){ + error = func + func = data.pop() } // ceck if we need to await... return data .reduce(function(res, e){ @@ -813,9 +879,23 @@ object.Mixin('PromiseMixin', 'soft', { // NOTE: we will not reach this on empty data... (data.length > 1 ? Promise.all(data) - .then(function(res){ - return func(...res) }) - : data[0].then(func)) + .then( + function(res){ + return func(...res) }, + ...(error ? + [error] + : [])) + : data[0].then( + func, + ...(error ? + [error] + : []))) + : error ? + function(){ + try{ + func(...data) + }catch(err){ + error(err) } }() : func(...data) }, }) diff --git a/README.md b/README.md index 6a096bf..547ac6a 100644 --- a/README.md +++ b/README.md @@ -2009,12 +2009,15 @@ Await for inputs if any of them is a promise and then run a function with the results, otherwise run the function in sync. ```dnf -Promise.awaitOrRun(, ) -Promise.awaitOrRun(, .. , ) +Promise.awaitOrRun(, [, ]) +Promise.awaitOrRun(, .. , [, ]) -> -> ``` +Note that if the last `` is a function and no `` function +is given then `.awaitOrRun(..)` will confuse the `` for ``, +to avoid this one needs to explicitly pass `null`/`undefined` as ``. ## Generator extensions and utilities diff --git a/package.json b/package.json index 4297734..67262da 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ig-types", - "version": "6.22.0", + "version": "6.22.1", "description": "Generic JavaScript types and type extensions...", "main": "main.js", "scripts": {