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

node-async-flow

v1.3.1

Published

An aggressive async flow solution for node.js.

Downloads

1,325

Readme

node-async-flow

An aggressive async flow solution for node.js.

The support for Generator is required.

Why?

node.js is excellent for it's advanced support for new standards. But it's been a headache as there are callbacks everywhere and it's hard to compose logic between them.

Peoples have been attempting to solve this situation for a long time:

The async provides handy utility functions to chain things together, along with a central error handing mechanism. It saves some nested callbacks but doesn't solve the problem.

There are also many Promise implementations, like q and bluebird. I personally dislike Promises because it's a standard which makes things harder: everything is in chainable callbacks and now we have a nice constructor and resolve and reject and then and catch, wow!

The co is the widely used generator-based solution for the callback hells. It's similar to the async/await standard and use Promises. To be used with co, everything should be Promises or "thunkified", which is another story.

Here comes the node-async-flow.

Features

  • No wrapper needed for existing code (compared to new Promise and thunkify)
  • No need to pass variables every next step
  • Async in native loops
  • In place error handling (if error is solvable let's continue)

Installation

npm install node-async-flow --save

Examples

See examples, tests, nwjs-download and nwjs-builder :)

Some code snippets:


const { exists, readFile } = require('fs');

const Flow = require('node-async-flow');

// Sleep 10 seconds.
Flow(function*(cb) {
  console.log('now:', Date.now());
  for(let i = 0; i < 10; i++) {
    yield setTimeout(cb.single, 1000);
  }
  console.log('now:', Date.now());
});

// Async check file exists and do something.
Flow(function*(cb) {
  if(yield exists('package.json', cb.single)) {
    console.log('File exists.');
  }
});

// Async read file and parse JSON.
Flow(function*(cb) {
  var [err, data] = yield readFile('package.json', { encoding: 'utf-8' }, cb.expect(2));
  if(err) return console.error(err);
  var json = JSON.parse(data);

  // or, in single line.
  var [err, json] = yield readFile('package.json', { encoding: 'utf-8' }, (err, data) => err ? cb.expect(2)(err) : cb.expect(2)(null, JSON.parse(data));
  if(err) return console.error(err);

  // or, use fs-extra.
  var [err, json] = yield require('fs-extra').readJson('package.json', cb.expect(2));
  if(err) return console.error(err);

});

// Destructuring.

const getOneValue = (callback) => setTimeout(() => callback(true), 0);
const getTwoValues = (callback) => setTimeout(() => callback(true, false), 0);
const getThreeValues = (callback) => setTimeout(() => callback(true, false, null), 0);

Flow(function*(cb) {
  var x = yield getOneValue(cb.single);
  // x == true.
  var x = yield getOneValue(cb.expect(1));
  // x == [true].
  var [x] = yield getOneValue(cb.expect(1));
  // x == true.
  var [x, y] = yield getTwoValues(cb.expect(1));
  // x == true, y == undefined.
  var [x, y] = yield getTwoValues(cb.expect(2));
  // x == true, y == false.
  var {z, y, x} = yield getThreeValues(cb.map('x', 'y', 'z'));
  // x == true, y == false, z == null.
});

License

MIT.