outcome

DRY error handling

var fs = require('fs');
 
fs.stat('some/file.js', function(errresult) {
    
    if(err) {
        //do stuff, or throw 
        return;
    }
 
 
    //do success stuff 
});

There are two ways you can use outcome.js:

 
 
var onOutcome = outcome().result(function() {
    
    console.log('result');
 
}).error(function() {
    
    console.log('error');   
});
 
 
fs.stat(__filename, onOutcome);

Or

 
outcome.call(fs.stat, __filename).result(function() {
 
    console.log("RESULT");
 
}).error(function() {
    
    console.log("ERROR");
 
})
 
  • listenersOrEm - listeners or eventEmitter
var em = new EventEmitter();
 
em.on('result', function() {
    console.log("RESULT");
});
 
fs.stat(__filename, outcome(em));

Or

var onOutcome = outcome({
    resultfunction() {
        console.log("RESULT");
    },
    errorfunction() {
        console.log("ERROR");
    }
});
 
fs.stat(__filename, onOutcome);

By default, any unhandled errors are thrown. To get around this, you'll need to listen for an unhandledError:

outcome.on('unhandledError', function(error) {
    //report bugs here..., then throw again. 
});
 
 
fs.stat(outcome({
    resultfunction(){}
}));

Calls the given function

  • fn - target function to be called
  • target - target scope - for this
outcome.call(fs.stat, null, fs).on({
    errorfunction() { },
    resultfunction() { }
});
 
//or 
outcome.call(fs.stat, fs, {
    errorfunction() { },
    resultfunction() { }
})

Listens for any events emitted by outcome - primarily unhandledError

  • typeOrEvent - type of event (string), or object of events
  • callback - callback for the listener
outcome.on('unhandledError', function() {
    //DO STUFF 
});

Listens to the given event emitter.

outcome.listen(em).on({
    someEventfunction(){}
});
 
//or 
 
outcome.listen(em, {
    someEventfunction(){}
});

Pipes events to target event emitter

outcome.call(fs.stat).emit(em);

Called when on error/result:

outcome.call(fs.stat, {
    resultfunction() {
        
    }
}).done(function(errresult) {
    
});

Called on success/result

outcome.call(fs.stat).result(function(result) {
    
});

Called on error

outcome.call(fs.stat).error(function(err) {
    //handle error 
});

Same as as above

Listens for results - error, and result primarily.

  • typeOrEvent - type of event (string), or object of events
  • callback - callback for the listener
outcome.call(fs.stat).on('error', function() {
    
}).on('result', function() {
    
});
 
//or  
 
outcome.call(fs.stat).on({
    errorfunction() {},
    resultfunction() {}
})