You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.5 KiB

  1. # cb()
  2. A minimal node.js utility for handling common (but often overlooked) callback scenarios.
  3. ##Features
  4. * `.timeout()`: Simple callback timeouts
  5. * `.error()`: Explicit error handling
  6. * `.once()`: Once-and-only-once callback semantics
  7. * Guaranteed asynchronous callback execution (protects against code that breaks this assumption)
  8. ## Installation
  9. $ npm install cb
  10. ## Usage
  11. ### Basic Usage
  12. The most basic usage of `cb` consists of passing in your own function reference. In this example, `cb` will do nothing other
  13. than insure the once-and-only-once, asynchronous invocation of the callback.
  14. doAsync(cb(function(err, res) {
  15. console.log(res);
  16. }));
  17. ### Timeout Handling
  18. Timeouts are specified through the `.timeout()` method, and are specified in milliseconds. If a timeout does occur, the error
  19. passed to the callback will be an instance of `cb.TimeoutError`.
  20. doReallySlowAsync(cb(function(err, res) {
  21. assert(err instanceof cb.TimeoutError);
  22. }).timeout(50));
  23. *Note: once a timeout has occured, any tardy attempts to invoke the callback will be ignored.*
  24. ### Explicit Error Handling
  25. In situations where it is convenient to separate the code that runs on success or failure, this can easily be accomplished
  26. with `.error()`. If an 'errback' handler has been provided to `.error()`, then it is assumed that the error-first parameter
  27. to the success handler is no longer required. To illustrate,
  28. doAsync(cb(function(err, res) {
  29. if (err) {
  30. console.error(err);
  31. } else {
  32. console.log(res);
  33. }
  34. }));
  35. Can be rewritten as:
  36. doAsync(cb(console.log).error(console.error));
  37. ### Force Once-and-only-once Callback Execution
  38. Sometimes it's necessary to ensure that a callback is invoked once, and no more. Once-and-only-once execution semantics can be
  39. enforced by using `.once()`.
  40. function runTwice(callback) {
  41. process.nextTick(function() {
  42. callback();
  43. callback();
  44. });
  45. }
  46. runTwice(cb(function() {
  47. console.log('I will only run once');
  48. }).once());
  49. *Note: technically, `.once()` simply enforces at-most-once semantics. However, when combined with `.timeout()`, once-and-only-once
  50. is achieved.*
  51. ### Combining Features
  52. The `cb` API is fully chainable, and any arrangement of the features is valid. For example:
  53. doAsync(cb(console.log).error(console.error).timeout(50).once());
  54. ## Running the Tests
  55. $ make test
  56. ## License
  57. The MIT License (MIT)
  58. Copyright (c) 2012 Jeremy Martin
  59. Permission 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:
  60. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  61. THE 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.