jakesgordon / javascript-state-machine

A javascript finite state machine library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

providing in config listener returning promise makes transition unusable

ciekawy opened this issue · comments

For

const config = {
  init: 'NEW',
  transitions: [{
    name: 'init', from: 'NEW', to: 'READY'
  }],
  methods: {
    onBeforeTransition: function () {
      // here any code always returning promise - with more sense than below ;)
      return Promise.resolve(true);
    }
  }
}

const sm = new StateMachine(config);

console.log(sm.state); // here state is 'none' - no way to wait until sm is ready to be used.

an idea would be to provide some method i.e. pendingTransition() returning promise to be able to act only when state machine is ready to be used.

if anyone having same issue - a workaround could be to do

    onBeforeTransition: function () {
      if(this.state === 'none') {
        // return immediately to allow transition to predefined initial state.
        return;
      }
      // do promise stuff
      return Promise.resolve(true);
    }