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

steppin

v1.1.0

Published

Wraps async-waterfall in a self-contained and useful way

Downloads

4

Readme

steppin Build Status

I don't find async's waterfall very useful because of it's requirement to pass on state to the next function. That makes the signature of each step unique, difficult to refactor, and error-prone.

async.waterfall uses eachOfSeries under-the-hood and so does this.

No external dependencies but it depends on (and bundles) async-es/eachOfSeries under the hood.

Installing

npm install steppin --save

How it works

A task/step signature is constant and always (state, next). The signature of next is always (err, result) and the contents of result become associated with the key passed to steppin.

var steppin = require('steppin');

function calculate(color, size, callback) {
  // fake lookup
  setTimeout(() => callback(null, {
    cost:   1299,
    tax:    200,
    total:  1499,
  }));
}

steppin({
  color: (state, next) => next(null, 'red'),
  size: (state, next) => next(null, 'small'),
  price: (state, next) => calculate(state.color, state.size, next),
}, (err, state) => {
  console.log('state', JSON.stringify(state, null, 2));
});

Output:

{
  "color": "red",
  "size": "small",
  "price": {
    "cost": 1299,
    "tax": 200,
    "total": 1499
  }
}

Optional input

let initialState = { currency: 'usd' }; // Warning: value is not copied and steppin will modify the contents of this object
steppin({}, initialState, callback)

No constraints on state

State is just a regular object and there is nothing stopping you from doing state.color = "red" in a task instead of returning it to next.

However, if it's easily possible to avoid that you should. Avoiding that makes it easily understood what's happening, where everything is coming from, and how it can be reordered or refactored.

Why is this good

Because consistency is good and allows for other things. For example, with the consistency, you could easily implement a timeout wrapper around your tasks or any other wrapper at any point in the future:

var wrapInTimeout = (maxWait, step) => {
  return (state, next) => {
    var timeout = setTimeout(() => next(new Error('timeout')), maxWait);
    var done = (err, result) => {
      clearTimeout(timeout);
      next(err, result);
    };
    step(state, done);
  };
};

steppin({
  one: wrapInTimeout(1000, (state, next) => {
    thirdPartyServiceRequest(userId, next);
  }),
})

Key ordering not guaranteed?

In theory, the hash passed to steppin does not have the execution order guaranteed. However, in practice, I'm unaware of any engines or environments that would not execute in the order defined.

If you're still concerned, you can refactor the above example into this and it'll work:

steppin([
  (state, next) => next(null, 'red'),
  (state, next) => next(null, 'small'),
  (state, next) => calculate(state.color, state.size, next),
], (err, state) => {
  console.log('state', JSON.stringify(state, null, 2));
});

The keys will be a string that is the index of the step in the array:

{
  "0": "red",
  "1": "small",
  "2": {
    "cost": 1299,
    "tax": 200,
    "total": 1499
  }
}