This maybe monad implemented with a Proxy. This monad let's you chain method
calls without relying on a higher order then/chain/andThen function
var{Maybe}=require("./index");functionapiRequest(){if(Math.random()<0.5){return{person: {name: 'bobby',projects: [{lang: 'elixir',name: 'brute'}]}};}else{return{projects: []};}}varlang=maybePerson.person.projects[0].lang.valueconsole.log(lang)Use value to return the current value. This returns the value or null.
andThen is still useful if wanting to use a function not implemented on the underlying value:
functionsomeOp(value){console.log("Performing some op",value)}maybePerson.person.projects.andThen(projects=>someOp(projects))Chaining methods together with will always return a new Maybe.
varmaybeProjects=maybePerson.projectsconsole.log(maybePerson.value)// prints null or valueconsole.log(maybeProjects.value)// prints null or valuemaybeProjects.andThen(value=>{// only prints value if value is not nullconsole.log(value)returnvalue// value must be return if chaining is to continue}).andThen(v=>someOp(v)).andThen(v=>someOtherOp(v))