diff --git a/Promise.js b/Promise.js index 7614003..357d711 100644 --- a/Promise.js +++ b/Promise.js @@ -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) diff --git a/README.md b/README.md index 6bc9b3b..6a096bf 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,8 @@ Library of JavaScript type extensions, types and utilities. - [Promise proxies](#promise-proxies) - [`.as(..)`](#promiseas) - [`.(..)`](#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 `` 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(, ) +Promise.awaitOrRun(, .. , ) + -> + -> +``` + + + ## Generator extensions and utilities ```javascript diff --git a/package.json b/package.json index 93837c2..4297734 100644 --- a/package.json +++ b/package.json @@ -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": {