Nodejitsu

Save time managing and deploying your node.js app

Nodejitsu has joined GoDaddy

We are excited to join GoDaddy to help spearhead their largest Node.js product: Website Builder. Please read our blog post to learn more about customer transition plans and our continued support for Open Source within the node community.

Want to know all the details?
Read the full blog post or read the GoDaddy Announcement.

npmawesome: simple async control flow with stepup

About the author

Name
Location
Worldwide
nodejitsu nodejitsu

Other popular posts

- Scaling Isomorphic Javascript Code - Keep a node.js server up with Forever - Package.json dependencies done right
- npm cheat sheet - 6 Must Have Node.js Modules

This is a guest post from Alex Gorbatchev over at npmawesome.com. Nodejitsu loved what npmawesome.com was doing and is now supporting the project.

stepup (GitHub: CrabDude/stepup, License: MIT) by Adam Crabtree is a simple control-flow library that makes parallel execution, serial execution, and error handling painless. This module originated as step by Tim Caswell and was pretty popular. I liked the stepup fork for the new features that it offers.

npm install stepup  

Usage

The most important thing to understand about stepup is that it basically creates and keeps track of callback functions that you can then pass around. Here's a simple example of reading the content of every file in the current directory.

var stepup = require('stepup');  
var fs = require('fs');

stepup([  
  function ($) {
    fs.readdir(__dirname, $.first());
  },
  function ($, files) {
    // pass down the list of files first
    $.first()(null, files);

    // create a group of `first` type of callbacks
    var stats = $.group('first');

    for (var i = 0; i < files.length; i++) {
      fs.stat(__dirname + '/' + files[i], stats());
    }
  },
  function ($, files, stats) {
    var content = $.group('first');

    for (var i = 0; i < files.length; i++) {
      if (!stats[i].isDirectory()) {
        fs.readFile(__dirname + '/' + files[i], content());
      }
    }
  },
  function ($, content) {
    return content.map(function (buf) {
      return buf.toString();
    });
  }
], function (err, content) {
  console.log(content.join('\n-----\n'));
});

What Else?

Checkout the runnable example and github example repository.