@akolos/object-promise-all

A strongly typed Promise.all-like function that works on objects that has promises as some of its properties.

Usage no npm install needed!

<script type="module">
  import akolosObjectPromiseAll from 'https://cdn.skypack.dev/@akolos/object-promise-all';
</script>

README

object-promise-all

Like Promise.all, but works on most objects, not just arrays.

Useful for creating a collection of labeled promises and doing something once they are all resolved. You don't have to worry about maintaining array indices like with Promise.all.

Usage

import promiseAll from '@akolos/object-promise-all';

const obj = {
    prop1: Promise.resolve('prop1'),
    prop2: Promise.resolve('prop2'),
};

const resolved = await promiseAll(obj);
// resolved equals {prop1: 'prop1', prop2: 'prop2'}

The function also works with nested objects, arrays, and non-promise members.

const obj = {
    prop1: Promise.resolve('prop1'),
    arr: [
            Promise.resolve('prop2'),
            { prop3: Promise.resolve('prop3')},
    ],
     notAPromise: 'prop4',
};

const resolved = await objectPromiseAll(obj);
/*
{
    prop1: 'prop1',
    arr: ['prop2', {prop3: 'prop3'}],
    notAPromise: 'prop4',
}
*/

Do note that functions will be lost in the output, since the library can't be sure that copying these functions is safe. If you disagree, please open an Issue to discuss.


const obj = {
    prop1: Promise.resolve('prop1'),
    date: new Date(),
    fn: () => 2,
    arr: [1, () => 1]
}

const resolved = await objectPromiseAll(obj);

/*
    typeof resolved = {
        prop1: string,
        date: {},
        arr: number[]
    }
 */