Skip to content

peachest/promise-props

Repository files navigation

promise-props

NPM VersionNPM DownloadsGitHub last commit (branch)GitHub License

GitHub Actions Workflow StatusGitHub Actions Workflow Status


A simple extension for promise

English · 简体中文

Usage · Report Bug · API Documentation

Stat

Table of Contents

Feature

  1. Provide propsDeep() to deal with object with promises nested in deep level
  2. Enable calling functions as other static methods on Promise by mounting withinit()
  3. Support ES6 module.
  4. Support typescript. Provide type declaration
  5. Provide detailed documentation including: README, tsDoc in IDE and API pages

Installation

npm install @pearden/promise-props
# or
yarn add @pearden/promise-props

[↑ back to top]

Quick 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,
    }
}
*/

[↑ back to top]

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.

[↑ back to top]

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

[↑ back to top]

Test

This project use jest for testing

yarn test

Check test directory for detail.

Build

This project use Rollup to build.

# build once
yarn build

# build with file watching
yarn build:watch

For detailed building configuration, check rollup config.

[↑ back to top]

Publish

Local publish

Publish NPM package with yarn

yarn publish --acccess public

Github Action

Auto publish to NPM when a Github release or prerelease is published.

Check for workflow config.

[↑ back to top]

See Also

petkaantonov/bluebird: 🐦 Bluebird is a full featured promise library with unmatched performance

sindresorhus/p-props: Like Promise.all() but for Map and Object

promise-props

Siilwyn/promise-all-props: Like Promise.all but for object properties.

magicdawn/promise.obj: promise.obj / promise.props

[↑ back to top]