@kennethlimcp, the delay(1) falls in line with the millis() test which won’t change for 1ms anyway. And don’t forget that, at best, the loop() thread runs every millisecond. As @rickkas7, points out, both delay(1) and os_thread_yeild() will allow other threads to run as the while() runs. Either way, loop() generally gets back control every ms or so.
The challenge is with your synchronous call of Particle.function() where you call a function and expect to return a (delayed) value as a result.
Another approach might be to decouple the status from the Particle.function() by having a Particle.variable() to represent the status. You could even decouple further and set a flag in the Particle.function() that loop() picks up and runs a small FSM to do the call_this_function(value), wait for the prescribed period and set the Particle.variable() status variable accordingly. You get the idea.
@kennethlimcp, not really since Particle.function() calls are serviced in the user thread. Depending on the calling server/service, you could have the FSM in loop() fire off a Particle.publish() to indicate the status so you don’t need to call the variable.
I did some testing and it seems like even with a delay(10), main loop() doesn’t seem to execute during that period.
Added a print statement to the Particle.function() and it simply executes in the while() loop and the variable that gets updates in loop() is always displaying false (even though on the hardware it has changed the lights when it became true)
Correct. The loop function will never execute while in a Particle.function handler or a code-based variable handler, because they are dispatched between calls to loop() from the application thread.
When in non-threaded mode, Particle.process() will keep the cloud connection alive, but in threaded mode it doesn’t do anything in this case. And in no case does loop() ever get called from inside Particle.process() or delay(), as that would often cause loop() to be reentrant, which would cause all sorts of trouble.
Do the asynch value checking in its own function that returns a negative response code called something like WOULD_BLOCK when your timer has not expired and the value has not been set as you expect.
After the timer has expired (or the value has been set correctly) only then return true or false according to your logic.
This routine would be called in loop().
setParam() “starts” the timer from a logical sense.