0.0.2 • Published 11 years ago

either-fantasy v0.0.2

Weekly downloads
359
License
-
Repository
github
Last release
11 years ago

either-fantasy

A fantasyland either implementation

var either = require('either-fantasy')


// # Right values gets acted on
var right = either.of('actOn ')
  .map(addSelf)
  .chain(function(v) { return either.of('Hello ' + v) })

console.log(right.value)
// => 'Hello actOn actOn'



// # Left values shortcuts
var left = either.left('passThrough')
  .map(addSelf)
  .chain(function(v) { return either.of('Hello ' + v) })

console.log(left.value)
// => 'passThrough'



// # Function util
console.log( either.safe(returns)().value )
// => 'Good'

console.log( either.safe(throws)().value )
// => 'Bad'



function addSelf(v) { return v + v }
function throws() { throw 'Bad' }
function returns() { return 'Good' }

Example use: JavaScript can only exit a function in one of two ways, either return something or throw something. This can be modeled with a either, where errors shortcuts chains and values gets acted on.