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

promise-flow-control

v1.0.7

Published

async.auto for promises

Downloads

133

Readme

PromiseFlowControl

This library basically provides the same functionality as async.auto but for bluebird promises.

You can pass functions that should be executed and define dependencies that need to be resolved before execution.

This solves the problem of a promise chain loosing context. Consider the following example:

getUsers()
    .then(users => {
        const owner = users.find(user => user.isOwner);
        return getMoreUserInfo(owner);
    })
    .then(moreInfo => {
        console.log(moreInfo);
        console.log(users); // How to access users here?
    })
PFC
    .props({

        users: () => getUsers(),

        owner: ['users', ({users}) => users.find(user => user.isOwner)]

        moreInfo: ['owner', ({owner}) => getMoreUserInfo(owner)],
        
        logUsersAndInfo: ['users', 'moreInfo', ({users, moreInfo}) => {
            console.log(moreInfo);
            console.log(users);
        }]
    })

Supported values

You can pass values, functions that return synchronously and functions that return promises.

PFC
    .props({

        syncValue: 'sync value',

        syncFn: function () {
            return 'sync fn';
        },

        asyncFn: function () {
            return new Promise(function (resolve, reject) {
                setTimeout(function () {
                    resolve('async fn');
                }, 0);
            });
        },

        fnWithDependencies: ['syncFn', 'asyncFn', function (results) {
            return results.syncFn + ' + ' + results.asyncFn;
        }]

    })
    .then(function (results) {
        console.log(results); // {syncValue: 'sync value', syncFn: 'sync fn', asyncFn: 'async fn', fnWithDependencies: 'sync fn + async fn'}
    });

Concurrency

By passing a number as the second argument, you can limit the number of functions that should be executed at the same time.

PFC.props(flowConfig, 2); // run at most 2 functions at the same time

Errors

Non existent dependencies

In the following example, a requires b to be passed but b does not exist. The returned promise will be rejected with PFC.ERRORS.NON_EXISTENT_DEPENDENCIES.

PFC
    .props({
        a: ['b', function () {}]
    })
    .catch(function (err) {
        err.code === PFC.ERRORS.NON_EXISTENT_DEPENDENCIES.code // true
    })

Cyclic dependencies

In the following example, a requires b and b requires a. A loop like this cannot be resolved properly, so the returned promise will be rejected with PFC.ERRORS.CYCLIC_DEPENDENCIES.

PFC
    .props({
        a: ['b', function () {}],
        b: ['a', function () {}]
    })
    .catch(function (err) {
        err.code === PFC.ERRORS.CYCLIC_DEPENDENCIES.code // true
    })