@pearden/promise-props
v0.0.3
Published
<img alt="NPM Version" src="https://img.shields.io/npm/v/%40pearden%2Fpromise-props?logo=npm"/><img alt="NPM Downloads" src="https://img.shields.io/npm/dt/%40pearden%2fpromise-props?label=total%20downloads">to deal with object with promises nested in deep level - Enable calling functions as other static methods on Promise by mounting with
init() - Support ES6 module.
- Support typescript. Provide type declaration
- Provide detailed documentation including: README, tsDoc in IDE and API pages
Installation
npm install @pearden/promise-props
# or
yarn add @pearden/promise-propsQuick Start
// mount props() and propsDeep() methods on Promise
init() ;
await Promise.props({
name: "promise",
age: Promise.resolve(123),
}) ;
/* output
{
name: "promise",
age: 123,
}
*/
await Promise.propsDeep({
sub: {
name: "promise",
age: Promise.resolve(123),
}
}) ;
/* output
{
sub: {
name: "promise",
age: 123,
}
}
*/Usage
Import
// Enable calling like Promise.props and Promise.propsDeep
// Do this in your main.[ts|js] file
init()
// or import whenever you want
import {props, propsDeep} from "promise-props"Props
Like Promise.all,but only deal with top level promise in the object passed in.
- Other top level none promise properties will be shallow copied to the result object
- Nonenumerable properties will be ignored.
PropsDeep
Deal with any level promise nested in the object passed into function.
FAQs
How props() is implemented
Notice: nonenumerable properties will always be ignored.
All the top level promise will be extract into an array. Then call Promise.all on this array. If all promises reslove, then the resolve value will be set into result object with the origin key of promise properties.
If any top level promise reject, as props() is an async function, it will automatically return an promise with the same reject reason.
Finally, other top level properties will be shallow copied to result object.
How propsDeep() is implemented
Use lodash to deep copy the object passed in as the result object
Use tarverse to traverse object filtering out all promise properties at any level and record the property path in the object.
Call Promise.all on promises array and get an array of all resloved value.
Use``object-path` lib to set value into result object with paths
Finally return the result
Test
This project use jest for testing
yarn testCheck test directory for detail.
Build
This project use Rollup to build.
# build once
yarn build
# build with file watching
yarn build:watchFor detailed building configuration, check rollup config.
Publish
Local publish
Publish NPM package with yarn
yarn publish --acccess publicGithub Action
Auto publish to NPM when a Github release or prerelease is published.
Check for workflow config.
See Also
petkaantonov/bluebird: :bird: Bluebird is a full featured promise library with unmatched performance
sindresorhus/p-props: Like Promise.all() but for Map and Object
Siilwyn/promise-all-props: Like Promise.all but for object properties.
