Socket
Socket
Sign inDemoInstall

async-context

Package Overview
Dependencies
1
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    async-context

Run async functions with provided context


Version published
Weekly downloads
3
increased by200%
Maintainers
1
Install size
62.3 kB
Created
Weekly downloads
 

Readme

Source

async-context

Provide a middleware-like context to a chain of async functions.

Description

I am a big fan of async.js but ever so often I need something that allows me to collect number of dependencies in a row.

This function combines the philosophy of async.js with a middleware-like context passing to each continuation function.

I often feel it is easier to write functions in a more generic way then having to think about how arguments get passed down when using waterfall, or retrieving items from the result array using async.each & co.

Install

npm install async-context

Usage

var context = require("async-context");

// optionally pass in a context as the first argument -
// otherwise it will be provided
context({}, function loadUserFromDb(ctx, next) {
        db.User.load(123, function(err, user) {
            ctx.user = user;
            next(err, user);
        });
    }, function loadStuffForUser(ctx, next) {
        db.Stuff.load(ctx.user.id, function(err, stuff) {
            next(err, _.merge(ctx, {
                stuff: stuff
            }));
        });
    },
    function checkSanityOfUser(ctx, next) {
        if (!ctx.user.crazy)
            ctx.user.ok = true;

        next(null, ctx);
    },
    function done(err, ctx) {
        if (err) throw "tantrums";

        // ctx == { user: user, stuff: stuff, ok: true }
});

FAQs

Last updated on 16 Aug 2015

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc