Onidle delayed for wakeup

I am using imp.onidle to go to deep sleep but want to trigger it after imp.wakeup fires. is this behavior possible?

here is my code:
const SLEEP_TIME=5.0;

// update non volatile table with iteration count
if ((“nv” in getroottable()) && (“iteration” in nv)) {
} else {
nv <- {iteration = 1};

function foo()
server.log(“done waiting”);

imp.wakeup(3, foo)

// Register with the server
imp.configure(“deepSleepTest”, [], []);
server.log("Execution run " + nv.iteration);
hardware.pin1.configure(DIGITAL_IN_WAKEUP); // configure wake-up pin

// Enter deep sleep
imp.onidle(function() { server.sleepfor(SLEEP_TIME); } );

i never see the done waiting message.

I think the issue is that you are putting the Imp too deep sleep e.g. processor shutdown & only waking on pin1/sleep_time. imp.wake will only run in shallow sleep/idle this diagram might help you ImpState.png

i was hoping the onidle would wait for any pending wakeups to finish before going to deep sleep. is there a way to do this or do i have to call sleepfor manually after i finish all of my processing?

You need to call imp.onidle from foo() in this case. A pending wakeup at some time in the future doesn’t prevent the imp from being idle before that point.