Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

promise-flow-control

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promise-flow-control

async.auto for promises

  • 1.0.7
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
14
decreased by-53.33%
Maintainers
1
Weekly downloads
 
Created
Source

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
    })

FAQs

Package last updated on 02 Sep 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc