🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

async-context

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

async-context

Run async functions with provided context

1.0.0
latest
Source
npm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
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

Package last updated on 16 Aug 2015

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