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 🙏

© 2025 – Pkg Stats / Ryan Hefner

node-processor

v0.0.4

Published

A simple flow wrapper for asynchronous function that can avoid callback hell on the basis of execution order.

Readme

A simple flow wrapper for asynchronous function that can avoid callback hell on the basis of execution order.

Table of Contents

Installation

npm install node-processor

let asynFlow = require('node-processor').asynFlow;
let Processor = require('node-processor').Processor;
let Flow = require('node-processor').Flow;

asynFlow

required params

funcs: single asynchronous function or asynchronous function array.

function asyn1(options, callback) {
    console.log('asyn1', options);
    setTimeout(callback, 1000, null, 'result1');
}
function asyn2(options, callback) {
    console.log('asyn2', options);
    setTimeout(callback, 1000, 'this is an error', 'result2');
}
function asyn3(options, callback) {
    console.log('asyn3', options);
    setTimeout(callback, 1000, null, 'result3');
}

let funcs1 = asyn1;
let funcs2 = [asyn1];
let funcs3 = [asyn1, asyn2, asyn3];

xargs: single object param or object param array.

let xarg1 = { name: 'xarg1' };
let xarg2 = { name: 'xarg2' };
let xarg3 = { name: 'xarg3' };

let xargs1 = xarg1;
let xargs2 = [xarg1];
let xargs3 = [xarg1, xarg2, xarg3];

optional params

  • ignoreError(default false):
    When error occurs in some stage of the process flow, left functions will be skipped and the callback will be called if it exists.
  • returnXargs(default false):
    When it is true, xargs itself will be passed to the callback as the result.

four features

support various process situation

  1. one asynchronous function with one xarg
asynFlow({funcs: funcs1, xargs: xargs1});
  1. one asynchronous function with multi xarg
asynFlow({funcs: funcs1, xargs: xargs3});
  1. multi asynchronous function with one xarg
asynFlow({funcs: funcs3, xargs: xargs1});
  1. multi asynchronous function with multi xarg
asynFlow({funcs: funcs3, xargs: xargs3});

But, if the number of funcs is not equal with the number of xargs and they are neither 1, the asynFlow will not execute any function.

support recording error and result of every function

asynFlow({funcs: funcs3, xargs: xargs3}, function(error,result){
  console.log(result);
  //result is an array contains three objects like this {error:null, result:'result'}.
});

support skipping left functions when error occurs

asynFlow({funcs: funcs3, xargs: xargs3}, function(error,result){
  console.log(error);
  //error is an instance of Error with one description like 'Error occured in step 2'
  console.log(result);
  //result is an array contains two objects.
});

If ignoreError is set to be true, asynFlow will ignore every error and execute to the last step.

support return xargs in itself

When the returnXargs is true, the number of xargs must be 1.

asynFlow({funcs: funcs3, xargs: xargs1, returnXargs:true}, function(error,result){
  console.log(result);
  //result is just xargs1.
});
asynFlow({funcs: funcs3, xargs: xargs2, returnXargs:true}, function(error,result){
  console.log(result);
  //result is xargs2[0].
});

Processor

In order to be compatible with synchronous function and to make a certain abstract of process flow, processor class emerges as the times require. One processor is an object which contains a name and a function with a flag to mark whether it is asynchronous.

let processor1 = new Processor({
  asyn: true,
  name: 'step1',
  func: function(options,callback){
    console.log('step1', options);
    setTimeout(callback, 1000, null, 'result1');
  }
});

If the function is a synchronous function, it will be wrapped to accept one callback.

let processor2 = new Processor({
  asyn: false,
  name: 'step2',
  func: function(options){
    console.log('step2', options);
    return 'something';
  }
});

Flow

In essence flow is a collection of processor. Flow can add or remove processor dynamically by array index.

let flow = new Flow();//support ignoreError and returnXargs. 
flow.add(processor1);
flow.add(0,processor2);
flow.remove(1);

Flow can create a new flow which only contains some of primary processors by filter function.

let newFlow = flow.filter(function(processor){
  return processos.asyn;
});

Obviously, flow can execute just like asynFlow.

flow.execute(xargs1,function(error,result){
  console.log(error);//depends on whether there is a error and ignoreError.
  console.log(result);////depends on returnXargs.
});