arnaucube
arnaucube/thoughtsPublic Self-hosted repository
thoughts/node_modules/jsonwebtoken/node_modules/cb/README.md
node_modules/jsonwebtoken/node_modules/cb/README.mdRaw
 1# cb()
 2
 3A minimal node.js utility for handling common (but often overlooked) callback scenarios.
 4
 5##Features
 6
 7  * `.timeout()`: Simple callback timeouts
 8  * `.error()`: Explicit error handling
 9  * `.once()`: Once-and-only-once callback semantics
10  *  Guaranteed asynchronous callback execution (protects against code that breaks this assumption)
11
12## Installation
13
14    $ npm install cb
15
16## Usage
17
18### Basic Usage
19
20The most basic usage of `cb` consists of passing in your own function reference. In this example, `cb` will do nothing other
21than insure the once-and-only-once, asynchronous invocation of the callback.
22
23    doAsync(cb(function(err, res) {
24        console.log(res);
25    }));
26
27### Timeout Handling
28
29Timeouts are specified through the `.timeout()` method, and are specified in milliseconds.  If a timeout does occur, the error
30passed to the callback will be an instance of `cb.TimeoutError`.
31
32    doReallySlowAsync(cb(function(err, res) {
33        assert(err instanceof cb.TimeoutError);
34    }).timeout(50));
35
36*Note: once a timeout has occured, any tardy attempts to invoke the callback will be ignored.*
37
38### Explicit Error Handling
39
40In situations where it is convenient to separate the code that runs on success or failure, this can easily be accomplished
41with `.error()`.  If an 'errback' handler has been provided to `.error()`, then it is assumed that the error-first parameter
42to the success handler is no longer required.  To illustrate,
43
44    doAsync(cb(function(err, res) {
45        if (err) {
46            console.error(err);
47        } else {
48            console.log(res);
49        }
50    }));
51
52Can be rewritten as:
53
54    doAsync(cb(console.log).error(console.error));
55
56### Force Once-and-only-once Callback Execution
57
58Sometimes it's necessary to ensure that a callback is invoked once, and no more. Once-and-only-once execution semantics can be
59enforced by using `.once()`.
60
61    function runTwice(callback) {
62        process.nextTick(function() {
63           callback();
64           callback(); 
65        }); 
66    }
67
68    runTwice(cb(function() {
69        console.log('I will only run once');
70    }).once());
71
72*Note: technically, `.once()` simply enforces at-most-once semantics. However, when combined with `.timeout()`, once-and-only-once
73is achieved.*
74
75### Combining Features
76
77The `cb` API is fully chainable, and any arrangement of the features is valid.  For example:
78
79    doAsync(cb(console.log).error(console.error).timeout(50).once());
80
81## Running the Tests
82
83    $ make test
84
85## License 
86
87The MIT License (MIT)
88
89Copyright (c) 2012 Jeremy Martin
90
91Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
92
93The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
94
95THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.