There are two main libraries for a finite state machine in js :
1/ Machina:
Very well documented, examples, supports two JavaScript message bus providers out of the box: postal.js and amplify.js.
2/ Javascript State Machine:
simpler and easier to use, perfect for “basic” usages.