/resolve-value

Deeply resolve promises in a value, including all properties of an object.

Primary LanguageJavaScriptMIT LicenseMIT

resolve-value Donate NPM version NPM monthly downloads NPM total downloads

Deeply resolve all promises in a value.

Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.

Install

Install with npm (requires Node.js >=10):

$ npm install --save resolve-value

Usage

const resolve = require('resolve-value');

Examples

assert.equal(await resolveValue(10), 10); //=> 10
assert.equal(await resolveValue(Promise.resolve(10)), 10); //=> 10

Functions

By default all functions are called.

assert.equal(await resolveValue(() => Promise.resolve(10)), 10); //=> 10
assert.equal(await resolveValue(async () => Promise.resolve(10)), 10); //=> 10

Pass a custom replacer function as the second argument to override this behavior.

const obj = { fn: () => 'do nothing', num: Promise.resolve(10) };

const result = await resolveValue(obj, (value, parent) => {
  if (parent && parent.someKey === true) {
    return value;
  }
  return value();
});

console.log(result.fn); //=> [Function: fn]

Objects

Resolves all property values, including functions.

const obj = { foo: async () => delay('bar', 10), num: Promise.resolve(10) };
const actual = await resolve(obj);
//=> { foo: 'bar', num: 10 }

Deeply nested

const obj = { foo: async () => delay('bar', 10), num: Promise.resolve(10) };
const arr = [obj, Promise.resolve(1), Promise.resolve(() => 2)];

const actual = await resolve(arr);
//=> [ { foo: 'bar', num: 10 }, 1, 2 ]

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Please read the contributing guide for advice on opening issues, pull requests, and coding standards.

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Author

Jon Schlinkert

License

Copyright © 2021, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.8.0, on April 19, 2021.