To resolve nested promises in object properties, arrays, arrays of object with promises etc.
It is like Bluebird's Promise.props
but with native promises, nested props, and arrays support!
Simply install through npm:
npm i resolve-nested-promises
From this package only one function is exported as a named export and a default export.
import { resolveNestedPromises } from 'resolve-nested-promises'
// or
import resolveNestedPromises from 'resolve-nested-promises'
// or
const resolveNestedPromises = require('resolve-nested-promises')
This function takes one parameter of any type, and always returns a Promise
that resolves to:
- That param - if it is a primitive
- Promise resolve value - if it is a promise
- Array of resolved values - if it is an array of promises (like
Promise.all
) - Object with resolved values in place of Promise properties (nested Promise properties too!) and rest of properties preserved.
- Any mix of nested objects, arrays, arrays with object. All promises resolved!
Check tests
folder for more examples!
resolveNestedPromises(null) // resolves to null
resolveNestedPromises('String value') // resolves to 'String value'
resolveNestedPromises([Promise.resolve('a'), Promise.resolve('b')]) // resolves to ['a', 'b']
resolveNestedPromises({ a: Promise.resolve('a'), b: 'b' }) // resolves to { a: 'a', b: 'b' }