cloudworker
cloudworker copied to clipboard
inconsistency: top-level setTimeout, others
Simple test case:
setTimeout(() => {}, 0)
This works in cloudworker but fails in CF workers, with this error:
Some functionality, such as asynchronous I/O, timeouts, and generating random values, can only be performed while handling a request
We should disallow the same behavior and add tests for all cases we can think of.
It fails in the preview but I'm sure it works in production, have you tried it?
Hmm. Just tossing a single setTimeout(() => {}, 0) into an existing worker script seems to not break, but actually relying on a top-level timeout to do anything doesn't seem to work. I deployed this with wrangler and it didn't work, for example:
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request));
});
const prom = new Promise(resolve =>
setTimeout(() => {
resolve("hello from async");
}, 0)
);
async function handleRequest(request) {
const data = await prom;
return new Response(data, { status: 200 });
}