# multiple-callbacks

> execute a callback after the execution of other callbacks

Latest version **1.1.1** (published 2015-03-22) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install multiple-callbacks
pnpm add multiple-callbacks
yarn add multiple-callbacks
bun add multiple-callbacks
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2015-03-22 |
| First published | 2015-01-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | David Bonet Montes |
| Maintainers | davidbm |
| Keywords | callback, callbacks, multiple, sync |

## Links

- npm: https://www.npmjs.com/package/multiple-callbacks
- Repository: https://github.com/DavidBM/MultipleCallbacks
- Issues: https://github.com/DavidBM/MultipleCallbacks/issues
- npm.io page: https://npm.io/package/multiple-callbacks

## Dependencies (1)

- [debug](https://npm.io/package/debug.md) ^2.1.1

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 1.1.1 (latest) — 2015-03-22
- 1.0.10 — 2015-02-05
- 1.0.9 — 2015-02-05
- 1.0.8 — 2015-02-04
- 1.0.6 — 2015-02-04
- 1.0.5 — 2015-01-07
- 1.0.4 — 2015-01-06
- 1.0.3 — 2015-01-05
- 1.0.2 — 2015-01-04
- 1.0.1 — 2015-01-03
- 1.0.0 — 2015-01-03

## README

MultipleCallbacks
=================

[![Build Status](https://travis-ci.org/DavidBM/MultipleCallbacks.svg)](https://travis-ci.org/DavidBM/MultipleCallbacks)

**Execute a callback after the execution of other callbacks.**

_Note: If you're reading this, probably you want to use [Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) 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.

``` javascript

	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:

``` javascript

	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](https://www.npmjs.com/package/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](https://www.npmjs.com/package/debug#conventions) 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:

``` javascript

	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:

``` javascript

	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.**

---
_Source: https://npm.io/package/multiple-callbacks · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
