mirror of
https://github.com/flynx/types.js.git
synced 2025-10-29 02:20:07 +00:00
added error handling to .awaitOrRun(..)...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
07f8dfcb29
commit
881df1be21
88
Promise.js
88
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) },
|
||||
})
|
||||
|
||||
|
||||
@ -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(<value>, <func>)
|
||||
Promise.awaitOrRun(<value>, .. , <func>)
|
||||
Promise.awaitOrRun(<value>, <func>[, <onerror>])
|
||||
Promise.awaitOrRun(<value>, .. , <func>[, <onerror>])
|
||||
-> <promise(value)>
|
||||
-> <value>
|
||||
```
|
||||
|
||||
Note that if the last `<value>` is a function and no `<onerror>` function
|
||||
is given then `.awaitOrRun(..)` will confuse the `<value>` for `<func>`,
|
||||
to avoid this one needs to explicitly pass `null`/`undefined` as `<onerror>`.
|
||||
|
||||
|
||||
## Generator extensions and utilities
|
||||
|
||||
@ -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": {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user