npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

super-resolve

v1.0.0

Published

Resolve (to Promise) an entire object, or any value

Downloads

134

Readme

super-resolve

Install

npm install --save super-resolve

super-resolve will work in browsers (with browserify, webpack, or rollup), and node. For browsers you might need a Promise polyfill/ponyfill. See how to use a Promise polyfill/ponyfill below.

Usage

import superResolve from 'super-resolve';
const log = v=>console.log(v);

superResolve(p({
    value1: 1,
    value2: 2
})).then(log);

superResolve({
    value1: p(1),
    value2: p(2),
    obj: {
        value3: p(3), value4: p(4)
    }
}).then(log);

let obj = {one: p(1)};
obj.that = obj;
superResolve(obj).then(console.log.bind(console));

function p(value){
    //Mock asynchronous data retrieval.
    return new Promise(res=>{
        setTimeout(()=>{
            res(value);
        }, 20);
    });
}

Explanation

super-resolve takes a value, and returns a promise.

Any value passed to super-resolve is traversed, and all promise values are resolved.

For instance if you pass an object that has properties with waiting promises those promises will resolved before super-resolve resolves to the object you passed in.

In simpler terms:

let myObject = {value: Promise.resolve('a value')};

superResolve(myObject)
.then(myObject=>{
    //myObject = {value: "a value"}
});

In that last example the property value resolves to the string 'a value' before superResolve resolves to myObject.

Any other properties which includes nested object(s) properties would also be resolved before myObject returns. Like:

let myObject = {
    value: Promise.resolve('a value'),
    nested: {
        prop: Promise.resolve('a nested value')
    }
};

superResolve(myObject)
.then(myObject=>{
    //myObject = {value: "a value", nested: {prop: "a nested value"}}
});

Yes really.

About

I like it when values just look like values don't you?

For some usages this library could be overkill. When you want to grab data from a lot of different data sources in a very tight time interval this library could be very useful.

Note that some values might not work correctly. The type checking mechanism in super-resolve has been tuned to work with almost all possible javascript types. Some native types might trip up super-resolve. Typically you won't have to worry about this since most data returned from a promise will be JSON compatible.

The best possible values to pass to super-resolve:

  • JSON compatible objects
  • Arrays
  • Numbers, strings, dates, and other javascript objects.
  • Objects you define

Values that might cause super-resolve to fail:

  • DOM nodes
  • Other host objects

API

superResolve(value, PromiseConstructor) -> promise

The value parameter can be any value except undefined. What ever you pass to value if it is a promise, or has promises it will all be resolved. If promises are found by superResolve, or not found the return value will still be what you expect.

superResolve will not resolve promises on the prototype. Promises on the prototype would be odd anyway.

PromiseConstructor should be a custom polyfill/ponyfill from a library that implements the Promises/A+ spec (e.g. Bluebird).

superResolve.promise

superResolve.promise is a static property that equals null. Set this to a promise constructor to change the custom promise implementation instead of using the PromiseConstructor parameter of superResolve().

Why?

Someone probably would have made it anyway. Several months before the first code was written for super-resolve I was planning on making making something like it. I can predict some usages for this library. As a parameter consumer for other libraries, or as a helper for complex applications that are data heavy. Both are things I plan to use it for.

Happy coding!