• Im am not completely sure, but there seems to be a simple solution with the already existing Espruino StateMachine module:

    Why no do the actual asynchronous work in the "enter" function of each state, and then trigger a state transition in the last "then" block of the chained Promises as follows:

    function enterOne() {
        console.log("Enter One...");
    
        new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve('good');
            }, 5000);
        })
        .then((result) => {
            sm.signal(result);
        })
        .catch(() => {
            sm.signal('bad');
        });
    };
    

    In the "signal" function of the respective state there could be code that decides - based on the parameter on the "signal" call (here: 'good' or 'bad') - to which new state to transition:

    function signalOne(result, e) { 
    
        console.log("signal one with parameters", result, e);
        if ('good' === result) {
            return {state:'Two', wait:(e)?e:0};
        } else if ('bad' === result) {
            return {state:'Error', wait:(e)?e:0};
        }
    };
    

    I think this will fit my needs.

About

Avatar for wklenk @wklenk started