async-context

1.0.0 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i async-context

Weekly Downloads

1

Version

1.0.0

License

ISC

Last publish

Collaborators

  • mvhenten