In a Worker I need to yield regularly to allow the event loop to schedule incoming message processing.—At the moment I do that with a new promise and setTimeout()
.
addEventListener('message', () => console.log('reading incoming message'));
async function work() {
while (true) {
// do a piece of work
await new Promise(resolve => setTimeout(resolve, 0));
// break if ordered via message
}
}
work();
Is there a compacter way to voluntarily yield processing?—It feels as if I’m missing something here: it looks like a super common task to do, but there is no “natural” way to do it?