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

give-me

v0.1.0

Published

A simple js library to manage deferred functions with a callbacks-style syntax

Downloads

8,411

Readme

give-me Build Status

NPM

How it works

Give-me takes care of executing deferred functions in parallel/sequence, and allows you to use your functions exactly as they are keeping the well-known callback style. The only convention is that the callback arguments need to be the last in every function. The callback object is an array of callback results.

Installation

npm install give-me

all(functions [, arguments] [, maxConcurrency], callback)

Runs an array of functions in parallel, and returns (with a callback) an array of callbacks in the same order when all the functions had been called.

var giveMe = require('give-me');

var a = function(callback){ setTimeout((function(){ callback('a'); }), 200); };
var b = function(callback){ setTimeout((function(){ callback('b'); }), 100); };

giveMe.all([a, b], function(result){
	console.log(result);
	// will display [['a'],['b']]
});

If functions need some parameters to work, they can be included in the optional "arguments" parameter. Just keep the callbacks in the end.

var giveMe = require('give-me');

var a = function(parameter1, parameter2, callback){ 
	setTimeout((function(){ 
		callback(parameter1 + ' ' + parameter2);
	}), 200); 
};

var b = function(parameter3, callback){ 
	setTimeout((function(){ 
		callback('hello', parameter3);
	}), 100); 
};

giveMe.all([a, b], [['hello', 'world'], ['hi']], function(result){
	console.log(result);
	// will display [['hello world'],['hello', 'hi']]
});

When tuple argument provided it tries to return 2 arrays of results. In case the first array (supposed to be the errors) is an array of empty values it is nullified (so that callback is a function(x, y) => null, array).

var giveMe = require('give-me');

var errorFunc = function(callback){ 
	setTimeout((function(){ callback('error'); }), 200); 
};

var successFunc = function(callback){ 
	setTimeout((function(){  callback(null, 'hello'); }), 100); 
};

giveMe.all([errorFunc, successFunc], function(errors, results){
	console.log(errors); // will display ['error', null]
	console.log(results); // will display [null, 'hello'];
});

giveMe.all([successFunc, successFunc], function(errors, results){
	console.log(errors); // will display null
	console.log(results); // will display ['hello', 'hello'];
});

any(functions [, arguments] [, conditionalFunction], callback)

Runs an array of functions in parallel, but returns (with a callback) just the fastest, ignoring all the other callbacks.

var giveMe = require('give-me');

var a = function(callback){ setTimeout((function(){ callback('a'); }), 2000); };
var b = function(callback){ setTimeout((function(){ callback('b'); }), 100); };

giveMe.any([a, b], function(result){
	console.log(result);
	// will display ['[Not processed yet]',['b']]
});

Using the optional 'conditionalFunction' parameter the callback will be called when the fastest callback will satisfy a requirement provided through a sync function (in the example above, the 'c' function is the fastest that satisfies the condition, the callback for function b is anyway appended as processed before but it does not satisfies the requirement).

var giveMe = require('give-me');

var a = function(param, callback){ setTimeout((function(){ callback(param) }), 200); }
var b = function(param, callback){ setTimeout((function(){ callback(param) }), 50); }
var c = function(param, callback){ setTimeout((function(){ callback(param) }), 100); }

giveMe.any([a, b, c], [true, false, true], function(itemCallback){
	return itemCallback[0] == true;
}, function(result){
	console.log(result);
	// will display ['[Not processed yet]', [false], [true]]
});

sequence(functions [, arguments], callback)

Runs an array of functions in sequence, and returns (with a callback) an array of callbacks in the same order when all the functions had been called.

var giveMe = require('give-me');

var a = function(callback){ setTimeout((function(){ callback('a'); }), 200); };
var b = function(callback){ setTimeout((function(){ callback('b'); }), 100); };

giveMe.sequence([a, b], function(result){
	console.log(result);
	// will display [['a'],['b']] after ~300ms
});

License

MIT