added Promise.awaitOrRun(..)

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2022-11-29 18:39:23 +03:00
parent d64b1f069a
commit 07f8dfcb29
3 changed files with 35 additions and 1 deletions

View File

@ -801,6 +801,22 @@ object.Mixin('PromiseMixin', 'soft', {
iter: IterablePromise,
interactive: InteractivePromise,
cooperative: CooperativePromise,
awaitOrRun: function(data, func){
data = [...arguments]
func = data.pop()
// ceck if we need to await...
return data
.reduce(function(res, e){
return res
|| e instanceof Promise }, false) ?
// 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))
: func(...data) },
})
PromiseMixin(Promise)

View File

@ -94,6 +94,8 @@ Library of JavaScript type extensions, types and utilities.
- [Promise proxies](#promise-proxies)
- [`<promise>.as(..)`](#promiseas)
- [`<promise-proxy>.<method>(..)`](#promise-proxymethod)
- [Promise utilities](#promise-utilities)
- [`Promise.awaitOrRun(..)`](#promiseawaitorrun)
- [Generator extensions and utilities](#generator-extensions-and-utilities)
- [The basics](#the-basics)
- [`generator.Generator`](#generatorgenerator)
@ -1999,6 +2001,22 @@ the main `<promise>` is resolved.
### Promise utilities
#### `Promise.awaitOrRun(..)`
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(value)>
-> <value>
```
## Generator extensions and utilities
```javascript

View File

@ -1,6 +1,6 @@
{
"name": "ig-types",
"version": "6.21.0",
"version": "6.22.0",
"description": "Generic JavaScript types and type extensions...",
"main": "main.js",
"scripts": {