A helper function that receives an object with a Promise in each property and returns a promise that resolves to an object with the same properties and the resolved values of the promises.
The returned promise is rejected in the following cases:
- The input argument is not an "object"
- At least one of the promises are rejected
- ES6 Promise supporting Javascript engine (browser or Node.js), or at least an ES6 Promise polyfill
- a node package manager installed (such as NPM or Yarn)
import promiseAllProperties from 'promise-all-properties';
const promisesObject = {
someProperty: Promise.resolve('resolve value'),
anotherProperty: Promise.resolve('another resolved value'),
};
const promise = promiseAllProperties(promisesObject);
promise.then((resolvedObject) => {
console.log(resolvedObject);
// {
// someProperty: 'resolve value',
// anotherProperty: 'another resolved value'
// }
});
This helper function works the same as promiseAllProperties
, except it uses Promise.allSettled
instead of Promise.all
. It is therefore possible to get the status of all the promises, regardless of how many of them were fulfilled or rejected.
Usage example:
import {promiseAllSettledProperties} from 'promise-all-properties';
const promisesObject = {
someProperty: Promise.resolve('resolve value'),
anotherProperty: Promise.reject(new Error('a rejection')),
yetAnotherProperty: Promise.reject(new Error('another rejection')),
};
const promise = promiseAllSettledProperties(promisesObject);
promise.then((resolvedObject) => {
console.log(resolvedObject);
// {
// someProperty: {status: 'fulfilled', value: 'resolve value'},
// anotherProperty: {status: 'rejected', reason: Error('a rejection')},
// yetAnotherProperty: {status: 'rejected', reason: Error('another rejection')}
// }
});
// By comparison, promiseAllProperties(promisesObject) would reject with Error('a rejection')
- Minimum Node.js version is now 12.20.0 to support the promiseAllSettledProperties method
- Passing an Array of values causes the promise to be rejected as invalid
- Stricter TypeScript signature now errors on non-object arguments.
npm test
npm run build
PR's are welcome just make sure the the PR is squashed (one commit) and the commit messages starts with one of the following prefixes:
[INITIAL]
: The initial commit
[FEAT]
: Only changes that creating new features or modofying existing features, that are not bug fixes
[FIX]
: Only bug fixes
[DOCS]
: Only Documentation changes
[STYLE]
: Only changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc)
[REFACTOR]
: Only code changes that are neither fixes or features
[TEST]
: Only changes that are adding new tests or modifying existing tests
[TOOLS]
: Only changes that affect external processeses like build tools, dev tools, auxiliary tools and libraries such as documentation generation
[CLEANUP]
: Only code removal: code lines, comment lines or files without affecting the project whatsoever
Public domain Unlicense