Last active
October 4, 2017 13:29
-
-
Save coderofsalvation/e198c613d51b13c4065fb5f2cac646bb to your computer and use it in GitHub Desktop.
lodash-fp-composition example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// BABEL/BROWSER import: | |
// import _ from lodash | |
// import {flow,when,error,lensOver,log,either,maybe} from lodash-fp-composition | |
// | |
// NODEJS import: | |
// var _ = require('lodash') | |
// _.mixin( require('lodash-fp-composition') ) | |
engine = { | |
inited:false, | |
user: false | |
} | |
engine.isInited = _.get('user') | |
engine.isNotInited = _.negate( engine.isInited ) | |
engine.getUser = fetch("/user/current",{method:"get"}) // =promise | |
engine.createUser = fetch("/user", {method:"post"}) // =promise | |
engine.getOrCreateUser = _.flow( | |
_.either( engine.getUser, engine.createUser ), | |
_.maybe( _.log("user ok") ) | |
) | |
engine.init = _.flow( | |
_.when( getOrCreateUser, _.lensOver( 'user', getOrCreateUser ) ), | |
_.when( engine.isInited, _.lensOver('inited', => true) ), | |
_.when( engine.isNotInited, _.error("something went wrong") ), | |
) | |
engine.init( _.clone(engine) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment