New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

multiple-callbacks

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

multiple-callbacks

execute a callback after the execution of other callbacks

latest
Source
npmnpm
Version
1.1.1
Version published
Maintainers
1
Created
Source

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.

Keywords

callback

FAQs

Package last updated on 22 Mar 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts