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

jschain

v0.0.3

Published

Chain style sequentially programming for Javascript asynchronous jobs.

Downloads

10

Readme

Javascript Chain

This is a small javascript function to rescue you from infinite callbacks.

Install

npm install jschain

Quick Start

var _map = 
{
	foo: function(next)
	{
		console.log('foo');
		setTimeout(next,1000);
	},
	bar: function(a,b,next)
	{
		console.log('bar: '+a+' '+b);
		setTimeout(next,1000);
	}
};

new JSChain(_map).foo().bar('hello','world').exec(function(next)
{
	console.log('cumtome function');
	setTimeout(next,1000);
}).end(function()
{
	console.log('done');
});

##When to use it?##

Javascript callbacks may looks like this:

ajaxGet('http://something',function(a)
{
	saveToDatabase(a,function(err)
	{
		if (!err)
		{
			notifyUser(123,function()
			{
				markNotified(123,function()
				{
					//done
				});
			});
		}
	})''
});

The traditional callback style is no longer suitable for heavy event driven style programming.

Now, see what JSChain can do:

new JSChain(
{
	foo: function(next)
	{
		console.log('foo');
		setTimeout(next,1000);
	},
	bar: function(a,b,next)
	{
		console.log('bar: '+a+' '+b);
		setTimeout(next,1000);
	}
}).foo().bar('hello','world').exec(function(next)
{
	console.log('cumtome function');
	setTimeout(next,1000);
}).end(function()
{
	console.log('done');
});

With the help of JSChain, the first demo code could be written like this:

var myController = 
{
	ajaxGet: function(next) {  ... ... next(); ... },
	saveToDatabase: function(a,next){ ... ... next(); },
	notifyUser: function() {  ... next(); },
	markNotified: function(next){ ... next(); ... }
};

new JSChain(myController).ajaxGet().saveToDatabase().notifyUser().markNotified();

And JSChain is very useful for data scraping projects:

function getURL(url,next)
{
	console.log('getting '+url);
	setTimeout(function()
	{
		console.log('done');
		next();
	},1000);
}


var chain = new JSChain({getURL: getURL});
for(var i=1;i<100;i++)
{
	chain.getURL('http://example.com/'+i);
}

The JSChain itself is very small. The full documented source code of JSChain is only 1.6KB. I can paste the source code here, so you can read the source code. This could help you understand how it works.

function JSChain(obj)
{
	var self = this;
	this.____items = []; //this remembers the callback functions list
	this.____finally = null;
	this.____next = function(jump)  //execute next function
	{
		if (!jump)
		{
			var func = self.____items.shift();
			if (func) func.call();
		}
		else self.____items = [];
		
		if (self.____items.length == 0 && typeof self.____finally == 'function')
		{
			self.____finally.call(obj);
			self.____finally = null;
		}
	};

	this.exec = function() //support for custom function
	{
		var args = [].slice.call(arguments,1),func = arguments[0];
		args.push(self.____next);
		self.____items.push(function()
		{
			func.apply(obj,args);
		});
		return self;
	};

	this.end = function(func) //support for final function
	{
		self.____finally = func;
		return self;
	};

	//copy and wrap the methods of obj
	for(var func in obj)
	{
		if (typeof obj[func] == 'function' && obj.hasOwnProperty(func))
		{
			(function(func)
			{
				self[func] = function()
				{
					var args = [].slice.call(arguments);
					args.push(self.____next); //pass next callback to the last argument
					self.____items.push(function() //wrap the function and push into callbacks array
					{
						obj[func].apply(obj,args);
					});
					return self; //always return JSChain it self like jQuery
				}
			})(func); // this is the closure tricks
		}
	}

	//start execute the chained functions in next tick
	setTimeout(self.____next,0);
	return this;
}

##Advanced features##

###end method###

An end method can add a function to the end of the chain. You can not define this in the object when new JSChian().

###jump to the end instead of going to next###

When you call next method, if you pass a true value into next() method, that means JUMP TO THE END.

###data passing along functions###

var obj = 
{
	a: function(i,next)
	{
		this.A = i.toUpperCase();
		next();
	},
	b: function(next)
	{
		this.B = this.A+'B';
		next();
	}
};
var c = new JSChain(obj);
c.a('a')
.b()
.exec(function()
{
	console.log(this);
});

this will output:

{ a: [Function], b: [Function], A: 'A', B: 'AB' }