promiseUnmap
Similar to Promise.map(ary) with some connotations:
-
If all promises resolve, the global promise resolves, that is identical.
-
All promises are going to be started, unmap won't
fulfill until all of them resolve or reject.
-
If any promises reject, promiseUnmap will reject with an Error containing all errors in its body as
err.errors: Array<Error>
. The message of the global error will be: "One or more tasks failed". Any fulfillments will also be insideerr.fulfillments: Array<any>
-
If it fulfills, it will resolve the map of fulfillments.
Usage
Either const {promiseUnmap} = require('promise-unmap')
or import {promiseUnmap} from 'promise-unmap'
It accepts an array of promises or functions that return promises.
const promiseUnmap = // ... const ops = async 'resolves 1' async { throw 'fails 2' } Promise async 'resolves 4' Promise async 'resolves 6' })
promiseUnmapSerial
Chain calls, return all results, throw if any errors.
Implementation
It is pretty obscure, read with caution. Basically consists on wrapping all promises with a safe catch, to later processing the results.
It is written in typescript. A type Future
is defined as a promise
or an async function (something that returns a promise). This way
we can warrantee that promises are not started until they are called.
type Future = Promise | Promise
A typeof
check is performed to know whether the future should
begin or it is an unfulfilled promise that we must await.
{ return Promiseallfutures}
The following method, promiseUnmapSerial
has been defined to enqueue such futures,
to safely execute a number of asynchronous functions, without interfering one another
(or await serially for promises to finish, but for that we already have
bluebird's mapSeries
)
{ const safePromises = futures return safePromises }