cloudworker icon indicating copy to clipboard operation
cloudworker copied to clipboard

inconsistency: top-level setTimeout, others

Open iameli opened this issue 5 years ago • 2 comments

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.

iameli avatar Mar 08 '20 05:03 iameli

It fails in the preview but I'm sure it works in production, have you tried it?

xtuc avatar Mar 09 '20 10:03 xtuc

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 });
}

iameli avatar Mar 09 '20 18:03 iameli