...critical? ...mainLoop smells Arduino loopy... It may be that there is no other way to easily convert this from a main-polling-loop over a bunch of flags that are set by interrupts of various participating sensors/inputs/... or a-like.
Each of these event producers - when written for an event driven system - allow to attach event handlers... as you see in practically all Espruino modules that provide connectivity to event producers. The reason is that the JavaScript engine - event queue server - is not allowed to block or take computing resources away to update a counter / time-comparison on the JavaScript level (because it would literally suck / hog, and more so with every additional such 'counter / timer'). If it blocks with the typical delay(...), it chokes / starves the hardware / low level service to death - and especially on a ESP8266, this is just not gona work.
I'm sure you had to move over also some of these in C written components that set global flags that are usually checked and handled in mainLoop constructs. Instead of setting such a global flag, allow to provide a callback.
You never can get completely away from polling... but if you have to do so, allow different intervals suitable to each of the components and do not enforce a single one thru the mainLoop.
In your high-level problem description you say A has to complete before B... Using promise is an elegant way to overcome call-back hell. Since you are talking of only two things with sequence dependency, a simple callback will do. I assume A is triggered by some event... and A has asynchronous behavior... (like the http operations). What you can do then is to take the (completion) event(s) of A and trigger B. With something like that you get completely rid of the mainLoop thing...
To be more detailed in the comments, I need a bit more details on the logic flow structure.
For your reading, take a look at sequencing implementations without / before availability of promises (using state machine concepts):
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
...critical? ...
mainLoop
smells Arduino loopy... It may be that there is no other way to easily convert this from a main-polling-loop over a bunch of flags that are set by interrupts of various participating sensors/inputs/... or a-like.Each of these event producers - when written for an event driven system - allow to attach event handlers... as you see in practically all Espruino modules that provide connectivity to event producers. The reason is that the JavaScript engine - event queue server - is not allowed to block or take computing resources away to update a counter / time-comparison on the JavaScript level (because it would literally suck / hog, and more so with every additional such 'counter / timer'). If it blocks with the typical
delay(...)
, it chokes / starves the hardware / low level service to death - and especially on a ESP8266, this is just not gona work.I'm sure you had to move over also some of these in C written components that set global flags that are usually checked and handled in
mainLoop
constructs. Instead of setting such a global flag, allow to provide a callback.You never can get completely away from polling... but if you have to do so, allow different intervals suitable to each of the components and do not enforce a single one thru the
mainLoop
.In your high-level problem description you say A has to complete before B... Using promise is an elegant way to overcome call-back hell. Since you are talking of only two things with sequence dependency, a simple callback will do. I assume A is triggered by some event... and A has asynchronous behavior... (like the http operations). What you can do then is to take the (completion) event(s) of A and trigger B. With something like that you get completely rid of the mainLoop thing...
To be more detailed in the comments, I need a bit more details on the logic flow structure.
For your reading, take a look at sequencing implementations without / before availability of promises (using state machine concepts):
First example explores the concepts, second one is an actual application where http is involved.