Finite state machines to deal with state in browser and node.js