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

step-resolver

v2.0.0

Published

Simple hierarchical resolver

Downloads

15

Readme

step-resolver

Simple, hierarchical promise resolver.

NPM Version Node Version

Install

npm install step-resolver

Usage

On several occasions I had the requirement of querying one store, if no result then querying the next and so on. A real example would be checking a memory cache, falling back to a database, falling back to a long-term filestorage system etc.

All functions are expected to return a promise and have the supplied arguments applied.

Sample Usage

The below example is one way of performing the above functionality, cache first, then database, then third party API. The after attributes of each resolver define what should happen if the method returns a truthy value. For instance, the cache resolver does not have an after function as there is no need, however if the cache resolver returns null and the database resolver returns a value, the after function of the database resolver could be to update the cache with the returned result.

const resolvers = [
    {
        method: cache.get
    },
    {
        method: (id, options) => {
            const newOptions = Object.assign(options, { authKey: process.env.SECRET_KEY });
            return database.fetch({ _id: id }, newOptions);
        },
        after: (result, { args }) => cache.set(args[0], result)
    },
    {
        method: service.fetchFromThirdParty,
        after: (result, { args }) => {
            return Promise.all([
                cache.set(args[0], result),
                database.set(args[0], result)
            ]);
        }
    }
];

const myResolver = Resolver(resolvers);
const result = await myResolver.attempt(id, options);

Logic

  • All resolver functions will be applied with the same parameters that attempt is called with.
  • If any resolver throws an error, the step-resolver will immediately reject with the error.
  • If all resolver functions have completed and there is still no result found, step-resolver will resolve with null.
  • If any resolver function resolves with a value, step-resolver will call the after function (if present) and return the result. An additional parameter is passed to the function allowing it access to the supplied arguments (as per example).

License

MIT