Skip to content

Instantly share code, notes, and snippets.

@arvitaly
Created September 20, 2016 09:57
Show Gist options
  • Save arvitaly/badf6cee1c62c993c654bfb705c5aa8b to your computer and use it in GitHub Desktop.
Save arvitaly/badf6cee1c62c993c654bfb705c5aa8b to your computer and use it in GitHub Desktop.
Example of change state in js-modules
var state = require('./state');
var f1 = require('./f1');
var f2 = require('./f2');
try{
f1();//Change state, really bad
var newState = f2();// pure function
//await f3
//yield* f4
}catch(e){
}
var state = require('./state');
module.exports = ()=>{
state.x = state.x + 1;
console.log(state.x); //side effect
}
var state = require('./state');
module.exports = ()=>{
state = Object.assign({}, state);
state.y = state.y + 2;
return state;
}
var state = {
x: 1,
y: 1
}
module.exports = state;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment