es6-callback-manager
Advanced tools
Comparing version 1.0.0 to 2.0.0
@@ -8,6 +8,8 @@ 'use strict'; | ||
* @class CallbackManager | ||
* @param {function} callback - The callback to invoke once all intermediary | ||
* callbacks have been invoked. Is invoked immediately if one of the | ||
* callbacks is called with an `Error` as the first argument and is passed | ||
* the `Error` object as the first argument. | ||
* @param {function} callback - The callback to invoke once all registered | ||
* callbacks have been invoked. Is called with either `null` or the | ||
* first `Error` that occurred as the first argument. | ||
* @param {boolean} [stopOnError=false] - If `true`, when an error is | ||
* encountered, the callback manager aborts and immediately invokes | ||
* `callback`. | ||
* @example | ||
@@ -23,7 +25,9 @@ * var CallbackManager = require('es6-callback-manager'); | ||
*/ | ||
constructor(callback) { | ||
constructor(callback, stopOnError) { | ||
this._callback = callback; | ||
this._count = 0; | ||
this._intermediaryCallback = error => { | ||
var error = null; | ||
this._intermediaryCallback = err => { | ||
if (this._count === 0) { | ||
@@ -33,10 +37,14 @@ return; | ||
if (error && error instanceof Error) { | ||
this.abort(); | ||
callback(error); | ||
return; | ||
if (err && err instanceof Error) { | ||
if (stopOnError) { | ||
this.abort(); | ||
callback(err); | ||
return; | ||
} | ||
error = error || err; | ||
} | ||
if (--this._count === 0) { | ||
callback(null); | ||
callback(error); | ||
error = null; | ||
} | ||
@@ -62,4 +70,4 @@ }; | ||
* waited on, it invokes the original callback. If it is called with an | ||
* `Error` as the first argument, it invokes the original callback | ||
* immediately with the `Error`. | ||
* `Error` as the first argument, the original callback will be invoked | ||
* with the `Error`. | ||
* @example | ||
@@ -74,3 +82,3 @@ * var cbManager = new CallbackManager(callback); | ||
* cb = cbManager.registerCallback(); | ||
* cb(error); // Stops waiting for other callbacks and calls callback(error) | ||
* cb(error); // The original callback will be called with this error | ||
*/ | ||
@@ -77,0 +85,0 @@ registerCallback() { |
The MIT License (MIT) | ||
Copyright (c) 2015 Nathan Woltman | ||
Copyright (c) 2015-2016 Nathan Woltman | ||
@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
{ | ||
"name": "es6-callback-manager", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "A class for managing asynchronous callbacks in JavaScript", | ||
"main": "es6-callback-manager.js", | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"files": [ | ||
@@ -30,17 +33,14 @@ "es6-callback-manager.js", | ||
"homepage": "https://github.com/woollybogger/es6-callback-manager#readme", | ||
"config": { | ||
"blanket": { | ||
"pattern": [ | ||
"es6-callback-manager.js" | ||
] | ||
} | ||
}, | ||
"devDependencies": { | ||
"grunt": "^0.4.5", | ||
"coveralls": "^2.11.4", | ||
"grunt": "~0.4.5", | ||
"grunt-eslint": "^17.3.1", | ||
"grunt-jsdoc-to-markdown": "^1.1.1", | ||
"grunt-mocha-cov": "^0.4.0", | ||
"mocha": "^2.3.3", | ||
"should": "^7.1.1" | ||
"grunt-mocha-istanbul": "^3.0.1", | ||
"grunt-mocha-test": "^0.12.7", | ||
"istanbul": "^0.4.1", | ||
"jit-grunt": "^0.9.1", | ||
"mocha": "^2.3.4", | ||
"should": "^8.0.1" | ||
} | ||
} |
@@ -17,10 +17,7 @@ # ES6 CallbackManager | ||
**Note:** Requires Node v4 or higher | ||
## Transpiling | ||
This module uses ECMAScript 2015 syntax (more widely known as ES6). If you need to use this module in an environment that only supports ES5 (such as a browser or an older version of Node.js), you'll need to transpile it from ES6 to ES5 using a transpiler such as [Babel](https://babeljs.io/). | ||
# API Reference | ||
## API Reference | ||
<a name="CallbackManager"></a> | ||
@@ -30,7 +27,7 @@ ## CallbackManager | ||
* [CallbackManager](#CallbackManager) | ||
* [new CallbackManager(callback)](#new_CallbackManager_new) | ||
* [.callback](#CallbackManager+callback) : <code>function</code> | ||
* [.registerCallback()](#CallbackManager+registerCallback) ⇒ <code>function</code> | ||
* [.getCount()](#CallbackManager+getCount) ⇒ <code>number</code> | ||
* [.abort()](#CallbackManager+abort) ⇒ <code>void</code> | ||
* [new CallbackManager(callback, [stopOnError])](#new_CallbackManager_new) | ||
* [.callback](#CallbackManager+callback) : <code>function</code> | ||
* [.registerCallback()](#CallbackManager+registerCallback) ⇒ <code>function</code> | ||
* [.getCount()](#CallbackManager+getCount) ⇒ <code>number</code> | ||
* [.abort()](#CallbackManager+abort) ⇒ <code>void</code> | ||
@@ -41,9 +38,10 @@ | ||
<a name="new_CallbackManager_new"></a> | ||
### new CallbackManager(callback) | ||
### new CallbackManager(callback, [stopOnError]) | ||
Creates a new CallbackManager. | ||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| callback | <code>function</code> | The callback to invoke once all intermediary callbacks have been invoked. Is invoked immediately if one of the callbacks is called with an `Error` as the first argument and is passed the `Error` object as the first argument. | | ||
| Param | Type | Default | Description | | ||
| --- | --- | --- | --- | | ||
| callback | <code>function</code> | | The callback to invoke once all registered callbacks have been invoked. Is called with either `null` or the first `Error` that occurred as the first argument. | | ||
| [stopOnError] | <code>boolean</code> | <code>false</code> | If `true`, when an error is encountered, the callback manager aborts and immediately invokes `callback`. | | ||
@@ -80,4 +78,4 @@ | ||
waited on, it invokes the original callback. If it is called with an | ||
`Error` as the first argument, it invokes the original callback | ||
immediately with the `Error`. | ||
`Error` as the first argument, the original callback will be invoked | ||
with the `Error`. | ||
@@ -94,3 +92,3 @@ **Example** | ||
cb = cbManager.registerCallback(); | ||
cb(error); // Stops waiting for other callbacks and calls callback(error) | ||
cb(error); // The original callback will be called with this error | ||
``` | ||
@@ -97,0 +95,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9795
111
10
130