callback-handler
This is a utility package to handle errors and unwanted results in callbacks automatically. It is used to improve readability of code and simplify working with callbacks.
Installation
$ npm install callback-handler
Usage
var CallbackHandler = ;
Version
The current version is 0.1.3
Features
- Simplified callbacks
- Automatic error handling
- Automatic handling of additional common callback use-cases
- Callbacks will never be triggered twice accidentally
- Well tested
- Extendable
Quick Examples
// Create a callback handler: var handler = callback; // Use `#next(callback)` instead of adding `if (err) { return callback(err); }` to the callback code: userModel; // Use `#nextIf(predicate, callback)` if you want to pre-check the result: { return any === null; } // Helper function chatRoomModel; // Use `#last()` to finish a callback chain and return the result: chatRoomModel; // See it altogether: { return any === null; } // Helper function { // calls back with (err, room) var handler = callback; userModel; }
Advanced Examples
// Use `#error(err)` to finish the callback chain and call the global callback with an error: handler; // Use `#success(data...)` to finish the callback chain and call the global callback with one or more (or none) results: { // calls back with (err, exists) - exists is either true or false var handler = callback; chatRoomModel; } // Or use `#finalize(err, data...)` if you prefer to be more explicit: { // calls back with (err, exists) - exists is either true or false var handler = callback; chatRoomModel; } // Use `#flatten(callback)` to use explicit variable names instead of working with array indices: async; // Use `#consume(callback)` if your callback returns a function that should be called just before the global callback is called: { // Unlock the database here } { // Lock the database here } { var handler = callback; ; } // Or use `#onFinalize(function)` if you want to be more explicit: { var handler = callback; ; }
Tests
Tests are written with mocha. Install the dev dependencies, then run npm test
:
$ npm install$ npm test