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

laps

v1.0.0

Published

Simple flow-control lib for Node.js: combine sync/async steps, organize your spaghetti-code

Downloads

4

Readme

Laps

Simple flow-control lib for Node.js: combine sync/async steps, organize your spaghetti-code

laps style:

new Laps()
.lap(function() {			
	nextPrime(startNum, this.cb('calc'));						
}).lap(function() {
	nextPrime(this.val, this.cb('calc'));
}).lap(function() {
	nextPrime(this.val, this.cb('calc'));
}).lap(function() {
	nextPrime(this.val, this.cb('calc'));
}).lap(function() {
	nextPrime(this.val, this.cb('calc'));
}).lap(function() {
	nextPrime(this.val, this.cb('calc'));
}).lap(function() {
	nextPrime(this.val, this.cb('calc'));
}).lap(function() {
	nextPrime(this.val, this.cb('calc'));
}).lap(function() {
	nextPrime(this.val, this.cb('calc'));
}).cmd('calc', function(val) {
	this.val = val;
}).on('finish', function() {
	console.log (this.val);
}).run();

Node.js style:

nextPrime(startNum, function(val){
	nextPrime(val, function(val){
		nextPrime(val, function(val){
			nextPrime(val, function(val){
				nextPrime(val, function(val){
					nextPrime(val, function(val){
						nextPrime(val, function(val){
							nextPrime(val, function(val){
								nextPrime(val, function(val){										
									console.log (val);
								});
							});
						});
					});
				});
			});
		});
	});
});

Installation

$ npm install laps

About

laps does not require anything from you. You can use it internally, you can return it as a result (promise-like), or you can use it eventually to reorganize some part of code.

It can work with any asynchronous API, and you do not need to wrap API method, you can wrap the callback.

The main thing you need to know: every lap is asynchronous internally but the sequence of laps will be executed synchronously. The next lap won't be started until the completion of all asynchronous callbacks of the previous lap. Every wrapped callback become the command and you can also create and execute the commands manually. In addition, you can subscribe to events: start of the execution, finish of the execution, begin of the lap, end of the lap, error.

Examples

Synchronous

It is often necessary to organize the sequence of asynchronous calls in a chain.

Consider this example:

var dns = require('dns');

var count = 0;
var handler = null;
var callback = function() {
	if (--count <= 0 && handler) {
		count = 0;
		handler();
	}
};
var error = function(err) {
	console.log('Error: ' + err.message);
	callback();
};
var reverse = function(type, adr) {
	return function(err, data) {
		if (err) return error(err);
		console.log(type + ', reverse for ' + adr + ': ' + JSON.stringify(data));
		callback();
	}
};
var resolve = function(type) {
	return function(err, addresses) {
		if (err) return error(err);
		for (var i = 0; i < addresses.length; i++) {
			var adr = addresses[i];
			++count;
			dns.reverse(adr, reverse(type, adr));            
		};
	}
};
handler = function () {
	handler = function () {
		handler = null;
		dns.resolve4('google.com', resolve('google'));
	};
	dns.resolve4('microsoft.com', resolve('microsoft'));		
};
dns.resolve4('unknown.unk', resolve('unknown'));

And here is the same code with laps:

var dns = require('dns');
var Laps = require('laps');

new Laps().lap(function() {
	dns.resolve4('unknown.unk', this.cb('resolve', 'unknown'));
}).lap(function() {
	dns.resolve4('microsoft.com', this.cb('resolve', 'microsoft'));
}).lap(function() {
	dns.resolve4('google.com', this.cb('resolve', 'google'));
}).cmd('resolve', function(type, err, addresses) {
	if (err) throw err;
	for (var i = 0; i < addresses.length; i++) {
		var adr = addresses[i];
		dns.reverse(adr, this.cb('reverse', type, adr));            
	};
}).cmd('reverse', function(type, adr, err, data) {
	if (err) throw err;
	console.log(type + ', reverse for ' + adr + ': ' + JSON.stringify(data));
}).on('error', function(err) {
	console.log('Error: ' + err.message);
}).run();

Asynchronous

The sequence of asynchronous calls can also be a problem if you need to process the final result only.

Here is the laps example for such situation:

var dns = require('dns');
var Laps = require('laps');

new Laps().lap(function() {
	this.results = {};
	dns.resolve4('unknown.unk', this.cb('resolve', 'unknown'));
	dns.resolve4('microsoft.com', this.cb('resolve', 'microsoft'));
	dns.resolve4('google.com', this.cb('resolve', 'google'));
}).cmd('resolve', function(type, err, addresses) {
	if (err) throw err;
	this.results[type] = [];
	for (var i = 0; i < addresses.length; i++) {
		var adr = addresses[i];
		dns.reverse(adr, this.cb('reverse', type, adr));            
	};
}).cmd('reverse', function(type, adr, err, data) {
	if (err) throw err;
	// Store your results
	this.results[type].push(data); 
}).on('error', function(err) {
	console.log('Error: ' + err.message);
}).on('finish', function(err) {
	// Process your results from all async callbacks
	// this.results ...
}).run();

Contributors

Author: MileAit

License

Public Domain - Unlicense

All the code is original, written from the scratch to avoid any possible license conflicts.