Outcome.js is a simple flow-control library which wraps your .callback(err, result)
functions.
Motiviation
- Write less code for handling errors.
- Easier to maintain.
- Keep error handling code separate.
Basic Example
Here's the traditional method of handling errors:
var fs = ; { fs; { iferr return ; fs; } { iferr return ; ; } }
The outcome.js way:
var fs = outcome = ; { //wrap the callback around an error handler so any errors in *this* function //bubble back up to the callback - I'm lazy and I don't wanna write this stuff... var on = outcome; //on success, call onRealPath. Any errors caught will be sent back //automatically fs; { //ONLY call onStat if we've successfuly grabbed the file stats fs; } { //no errors, so send a response back ; }} var on = outcome); ;
API
outcome(listeners)
listeners
- Object of listeners you want to attach to outcome.
var onResult =
As shown in the example above, you can also wrap-around an existing callback:
var onResult = outcome;
By default, any unhandled errors are thrown. To get around this, you'll need to listen for an unhandledError
:
outcome; //failsfs
.callback()
Called when on error/success. Same as function(err, data) { }
Here's a redundant example:
fs
.success(fn)
Called on Success.
var onOutcome = outcome; ;
.error(fn)
Called on error.
var onOutcome = outcome; ;
.handle(fn)
Custom response handler
outcome;
CoffeeScript Example
outcome = require "outcome" doSomething on = outcomeerror callback # first get the realpath fsrealpath pathonsuccess onRealPath # on real path, get stats onRealPath fslstat pathonsuccess onStat # on stat, finish onStat callback nullstats # call do something doSomething '/path/to/something'outcome : # do something : # do something else
Note
Calling .error()
, .success()
, .callback()
generates a new function which copies the previous listeners.
Checkout fs-test in the examples folder.