1.1.1 • Published 9 years ago

multiple-callbacks v1.1.1

Weekly downloads
5
License
MIT
Repository
github
Last release
9 years ago

MultipleCallbacks

Build Status

Execute a callback after the execution of other callbacks.

Note: If you're reading this, probably you want to use Promises instead of this library

Usage

Install with npm install multiple-callbacks

execute multipleCallbacks with the quantity of callbacks that will be executed before and the callback.

multipleCallbacks(times, callback, name); return a function. Every time that this function is executed count one callback as executed.

	var multipleCallbacks = require('multiple-callbacks');

	var cb = multipleCallbacks(2, makeHamburgers);

	getMeat();
	getBread();

	function getMeat () {
		/*Async operation*/
		setTimeout(cb, 1000);
	}

	function getBread () {
		/*Async operation*/
		setTimeout(cb, 1000);
	}

	function makeHamburgers () {
		console.log('I’m lovin’ it!');
	}

You can change the execution times needed to execute the callback at any time with:

	var multipleCallbacks = require('multiple-callbacks');

	var cb = multipleCallbacks(times, callback);

	cb.setTimesToFire(newTimes);

Other methods are:

getTimesToFire: Return the needed quantity of callbacks executions to execute your callback.

getFiredTimes: Return the times that the callback returned by MultipleCallbacks has been fired.

sumTimesToFire: Sum a quantity to the needed quantity of callbacks executions to execute your callback.

For debbug porposes you can pass a name to the constructor. multipleCallbacks(times, callback, name); It uses de debug module for show the name on every callback execution.

The name of debug function (for view the log) are MultipleCallbacks:log and MultipleCallbacks:warning. See the debug documentation for more info.

Indeterminated quantity of callbacks executions

If you don't know the quantity of times that the callback will be executed, you can pass false to 'multiple-callbacks' to cancel the execution on the final callback until you provide the quantity of calls to execute the final callback.

A weird example:

	var multipleCallbacks = require('multiple-callbacks');

	var exTime = 0;

	var callback = multipleCallbacks(false, function () {
		done();
	});

	services.forEach(function (service) {
		if(service.updated === false){
			service.doSyncOrAsyncStuff(callback)
			exTime++;
		}
	});

	cb.setTimesToFire(exTime);

Your async functions return data

Let's view the first example with data:

	var multipleCallbacks = require('multiple-callbacks');

	var cb = multipleCallbacks(2, makeHamburgers);

	getMeat();
	getBread();

	function getMeat () {
		/*Async operation*/
		setTimeout(function () {
			cb(new Meat());
		}, 1000);
	}

	function getBread () {
		/*Async operation*/
		setTimeout(function () {
			cb(new Bread());
		}, 1000);
	}

	function makeHamburgers (ingredients) {
		var hamburger = new Hamburger();

		for (var i = ingredients.length - 1; i >= 0; i--) {
			var ingredient = ingredients[i];
			hamburger.addIngredient(ingredient);
		}

		console.log('I’m lovin’ it!');
	}

If only one callback has returned data, multipleCallbacks don't pass an Array to the user callback. Instead pass the data itself.

1.1.1

9 years ago

1.0.10

9 years ago

1.0.9

9 years ago

1.0.8

9 years ago

1.0.6

9 years ago

1.0.5

9 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago