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

simple-control

v1.0.0

Published

Minimal async flow control functions

Downloads

4

Readme

simple-control

Minimal async flow control functions

Installation

npm install simple-control

Examples

Set up

const control = require('simple-control');

// Some async function you have defined...
function asyncFn(param, cb) {
  setTimeout(function() {
    cb(null, param * 2);
  }, 1000);
}

Vanilla runSeries:

control.runSeries([
  function(callback) {
    asyncFn(2, callback);
  },
  function(callback) {
    asyncFn(3, callback);
  },
  function(callback) {
    asyncFn(4, callback);
  }
], function(err, results) {
  if (err) console.log(err);

  else console.log(results); // --> [4, 6, 8]
});

A "waterfall" approach using runSeries:

control.runSeries([
  function(callback) {
    asyncFn(2, callback);
  },
  function(callback, results) {
    console.log(results); // [4]
    asyncFn(results[0], callback);
  },
  function(callback) {
    console.log(results); // [4, 8]
    asyncFn(results[1], callback);
  }
], function(err, results) {
  if (err) console.log(err);

  else console.log(results); // --> [4, 8, 16]
});

runParallel:

control.runParallel([
  function(callback) {
    asyncFn(2, callback);
  },
  function(callback) {
    asyncFn(3, callback);
  },
  function(callback) {
    asyncFn(4, callback);
  }
], function(err, results) {
  if (err) console.log(err);

  else console.log(results); // --> [4, 6, 8] (order not guaranteed!)
});

Methodology

These are the bare essentials for async control flow: run an array of async functions in series or run them in parallel. Running functions in series allows you to access results along the way (waterfall) and exit early if something goes wrong. Running in parallel gives you a speed boost but does not allow for interim access to results or an early exit on error. It can also be resource intensive if you launch a large number of tasks.

Most async tasks can be adequately managed with either of these two approaches.

Test

npm test to run the unit tests.

npm run dev-test to run tests in dev mode (so you can see console.logs and all the subtest checks).

API

runSeries(fns, done)

Params

fns

{Array} An array of functions to run. Each function takes a required callback {Function} and optional results {Array}. callback should be invoked on completion of your async operation. See examples above.

done

{Function} A final callback function to invoke once all scheduled functions have completed. Will be invoked immediately with an error and no subsequent functions will be run if any scheduled function errors out, otherwise will be invoked with the final results array (see examples above).

runParallel(fns, done)

Same API as runSeries except interim results are not available in runParallel. Results are only available in the final done callback.

control.runParallel([
  ...
  function(callback, results) {
    console.log(results); // NO! Will be undefined.
  }
  ...
], function(err, results) {
  if (err) console.log(err);
  else console.log(results) // This will work.
});

All functions are launched immediately and there is no guarantee the results will be in any particular order. If an error occurs, all results will be discarded and done will be invoked with the error. Note that unlike runSeries, all functions will still be executed even if one errors out.

License

MIT