• There is already a simple State Machine implementation in Espruino:
    https://www.espruino.com/modules/StateMa­chine.js

    State transitions are triggered by calling

    sm.signal();
    

    However, this expects that the "signal" function of the respective current state synchronously executes some operations and then returns the information what should be the next state to transition to. But how can I handle it if the "signal" function does some asynchronous operations (based on chained Promises). In this case, I cannot immediately decide what the next state should be. I just could return nothing, but then there would be no state transition. I cannot "await" the chained Promises. Actually, I don't see a way to do the state transition at the end of the Promises chain.

    Any clever ideas?

    Update 2019-01-08 : After trying out different approaches I think I could emit events. So the state transitions would be somehow triggered by the receipt of events. And it is no problem to emit events in chained Promises.

About

Avatar for wklenk @wklenk started