So in a notebook I want debug some code in a generator cell.
Say I have a loop like
{
for(let i=0;i<10;i++){
yield i;
}
}
This is great for seeing what’s going on but javascript is too fast! I want to slow it down so I can see the loop step through but when I throw in a timeout or something with await to force stopping I get the error “async not allowed in generators”.
Whats the best way of going about slowing down execution of a generator in this way?
Promises.delay, Promises.tick and Promises.when all use setTimeout because typically you want to sleep for longer than one animation frame. If you just want to sleep for one animation frame, then yield without any promise is the simplest option:
{
for (let i = 0; i < 10; ++i) {
yield i;
}
}
What makes Promises.tick synchronized is that it uses Promises.when under the hood, which allows multiple promises that want to wake up at the same time to use the same setTimeout internally:
Blundered my way to this via the Custom Generators writeup,
slowloop = {
let i = 0,
looper;
let slowcoreloop = runSimulation(osc2NodeExample());
for await (looper of slowcoreloop) {
yield { count: i, main: looper };
i++;
await Promises.delay(500);
}
}
Where async function* runSimulation(net) {…etc defines (if I have my terminology right) the generator function. I had trouble using a generator instantiated already in another cell, but this is progress at least…