npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

serialchain

v0.0.8

Published

A way to serialize/chain async functions.

Downloads

3

Readme

SerialChain Build Status

NPM

Description

SerialChain is a module I wrote to so that I could write pretty code.

I like writing chains and really wanted to be able to do it with asynch code. There are a couple other options out there that already do this, but I wanted to learn how to write my own so I made this one.

Installation

npm install serialchain

npm install ben-bradley/serialchain

Concepts & Use

  • You create a chain:
var chain = new SerialChain({ methodA: function(a, done) {
  // do something
  done(err, a);
}})
  • You add() links to the chain.
chain.add('methodB', function(b, done) {
  // do something else
  done(err, b);
});
  • You can then compose and execute your chain
chain
  .methodB('xyz')
  .methodA('abc').timeout(1500)
  .done(function(err, results) {
    if (err)
      throw err;
    console.log(results); // => [ 'xyz', 'abc' ]
  });

this.locals

I found that I needed to have an easy way to pass variables between my chained methods so I added the locals property/namespace to the chain.

var chain = new SerialChain();

chain.add('methodA', function (done) {
  var locals = this.locals;
  // do something async to produce a value to pass to the next method
  setTimeout(function() {
    locals.a = 'produced value';
  }, 100);
  done(); // nothing is returned
});

chain.add('methodB', function (done) {
  if (this.locals.a)
    this.locals.b = 'methodA() was called first';
  else
    this.locals.b = 'methodB() was called first';
  done(); // nothing is returned
});

chain
  .methodB()
  .methodA()
  .done(function (err, results) {
    console.log(results); // => [] because neither returned anything
    console.log(this.locals); // =>
    // { a: 'produced value', b: 'methodB() was called first' }
  });

Built-in Methods

  • SerialChain()

    • Returns a new chain.
    • Accepts an Object to add to the chain:
    var chain = new SerialChain({ methodName: callback });
    • Added method callbacks have the signature:
    function([arguments, from, chain, call, ]done) {
      // ...
    }
  • add()

    • This method adds other methods (links) to your chain
    • Accepts arguments as an Object:
    chain.add({ methodName: callback });
    • Also accepts String and Function
    chain.add('methodName', callback);
    • Added method callbacks have the signature:
    function([arguments, from, chain, call, ]done) {
      // ...
    }
  • timeout()

    • Calling this method after an add()ed method will set a timeout on the previously called method.
    • Arguments are a Number of ms to wait
      chain
        .methodA('a').timeout(1000) // will wait 1 second and bail
        .methodB('b')
        .done(function(err, results) {
          // ...
        });
  • done()

    • This method triggers the execution of the chain.
    • Arguments are a callback:
      chain
        .methodA('a')
        .methodB('b')
        .done(function(err, results) {
          // ...
        })

Example

var SerialChain = require('serialchain');

var chain = new SerialChain({
  thingOne: function (a, done) {
    setTimeout(function () {
      done(null, a + '-thingOne');
    }, 100);
  },
  makeError: function (err, done) {
    done(new Error(err));
  }
});

chain.add('returnOne', function (a, done) {
  setTimeout(function () {
    done(null, a);
  }, 500);
});

chain.add('returnTwoThree', function (a, b, done) {
  setTimeout(function () {
    done(null, a + b);
  }, 1500);
});

chain.add({
  blargh: function (done) {
    done(null, 'honk');
  }
});

chain
  .returnOne('one')
  // thingOne() will complete in 100ms so this timeout will pass
  .thingOne('1').timeout(1000)
  .blargh()
  .returnTwoThree('two', 'three')
  .done(function (err, results) {
    console.log(arguments);
  });

Tests

$ npm install && npm test

Version History

  • 0.0.8 - Ensured that errors are promoted to be a new Error()
  • 0.0.7 - Added code to clean up _chain when done() is called
  • 0.0.6 - Added locals namespace to pass vars between methods
  • 0.0.5 - Calling done() without args in an add()ed method doesn't populate results.
  • 0.0.4 - Version bump, added Travis-CI.
  • 0.0.3 - Added timeout().
  • 0.0.2 - Refactored to serialchain.
  • 0.0.1 - Removed async dependency.
  • 0.0.0 - Initial drop.

Other Options

  • continue - https://www.npmjs.org/package/continue - I took a lot of design ideas from this package, but it operates against a collection.