Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dangerous Behavior #70

Closed
tech-a-go-go opened this issue Sep 2, 2017 · 1 comment
Closed

Dangerous Behavior #70

tech-a-go-go opened this issue Sep 2, 2017 · 1 comment

Comments

@tech-a-go-go
Copy link

tech-a-go-go commented Sep 2, 2017

Hi there,

Please take a look at this simple code.

const Promise = require('bluebird');
const StateMachine = require('fsm-as-promised');
StateMachine.Promise = Promise;

var fsm = StateMachine({
    initial: 'here',
    events: [
      { name: 'jump', from: 'here', to: 'sky' },
      { name: 'run',  from: 'here', to: 'office' },
    ],
    callbacks: {
      onjump: function (options) {
        return new Promise(function (resolve, reject) {
          resolve(options);
        });
      }
    }
  });

fsm.jump().then((a) => {
  console.log('jump', fsm.current, a);
}).catch((err) => {
  console.log('err jump', err);
});

fsm.run().then((a) => {
  console.log('run', fsm.current, a);
}).catch((err) => {
  console.log('err run', err);
});

I thought fsm.jump() will succeed and fsm's state will be sky and fsm.run() will fail because fsm's state has already been changed to sky.

But the result is as follows.

jump office { name: 'jump', from: 'here', to: 'sky', args: [] }
run office { name: 'run', from: 'here', to: 'office', args: [] }

jump office !?

jump can only go to the sky...

I know jump() and run() are not connected with a promise chain so while jump() was being processed, run() was executed and run()'s result was taken effect at last.
I believe run() should throw an error or return a promise reject when it's called while other events are being processed..

I think this is very dangerous.

Cheers

@tech-a-go-go tech-a-go-go changed the title Weird Behavior Dangerous Behavior Sep 2, 2017
@vstirbu vstirbu closed this as completed in e245a72 Sep 8, 2017
@vstirbu
Copy link
Owner

vstirbu commented Sep 9, 2017

@tech-a-go-go thanks for spotting this behaviour! it was not just dangerous, it was plain faulty...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants