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

acnsy

v2.0.1

Published

rethinked async

Downloads

6

Readme

rAsync - async rethinked

This module is a wrapper to the popular async module, providing an easy way to combine serial/waterfall and parallel task execution. With this you can define a workflow of function calls using nested arrays. It helps to your code keep simple and readable.

Usage

var rAsync = require('r-async');
rAsync(initialData /* object (optional) */, tasks /* array */, finally /* function */);

The r-async module gives you a single function, which requires three parameters:

  • initialData : object (optional) - you can pass initial data to your tasks, this will be passed to the first codes to be executed as parameter (see below)
  • tasks : array - pass the functions, that you would like to execute. The given funcions will execute in series, while nesting further arrays of function calls will alternate between parallel and series execution, as you go more deep in nesting (see the example)
  • finally : function - pass a function here, which gets executed after the tasks, whether there was any errors or everything run fine

The tasks

Each task must be defined in the following form, recieving two parameters

function(params /* object */, callback /* function */){ ... }
  • params : object - parameters recieved from previously executed tasks or initialData
  • callback : function - you must execute this, to mark, that the task is finished. If you don't call the callback the process is breaked and the other tasks doesn't calls.

The callback must be called with two parameters:

callback(error /* null or any */, params /* object */)
  • error : Any (for example Error object) or null - if there was any errors during the execution of the task, then this should recieve the Error object, or any truthy value which describing the error, else it should be null
  • params : object - the recieved, and optionally altered params object should be sent back to rAsync through this

Defining serial and parallel execution

Let's look at the task definitions through an example. Supposingly we have five functions, as seen below:

function A(params, callback){
	console.log('A');
	callback(null, params);
}
function B(params, callback){
	console.log('B');
	callback(null, params);
}
function C(params, callback){
	console.log('C');
	callback(null, params);
}
function D(params, callback){
	console.log('D');
	callback(null, params);
}
function E(params, callback){
	console.log('E');
	callback(null, params);
}

By default, if we just dump these into an array, then rAsync will execute these in series:

var tasks = [A, B, C, D, E]; // A -> B -> C -> D -> E
rAsync({}, tasks, function(){});

If we introduce an array in the array of tasks, then it's contents will execute in parallel:

var tasks = [A, [B, C], D, E]; // A --> B ---> D -> E
                                    \-> C -/

Another level of nesting will provide us with changing back to serial execution and further nesting will keep shifting from the two modes.

var tasks = [A, [B, [C, D], E]; // A ---> B ----> E
                                    \-> C -> D -/

It is not possible to change the initial mode of execution, but we can always wrap the tasks in multiple arrays and there is no limit for how deep the nesting can go.

var tasks = [[A, B, C, D, E]]; // all executed in parallel

Conditional execution

//...require your modules
var rAsync = require('r-async');
rAsync({}, [
    yourFirstTask,
    rAsync.ifTask(yourConditionTask, yourTrueBranchTask, yourElseBranchTask)
    ],function(err,params){
        console.log(err,params)
    });

Where yourConditionTask must fill the ifresult property in the params. For example:

function yourConditionTask(params, callback){
    params.ifresult = params.xy==42; //calculate your result of condition
    callback(null, params);
}