Is there a time limit for setTimeout?


can anybody tell me if there is a time limit for setTimeouts if you call setTimeout from inside a new Promise that is passed to FetchEvent.waitUntil(...)?

A demo of the concept is here:


Code is here:

let inMemoryCache = 0;

function postProduction(e, cb, delay){
  e.waitUntil(new Promise(function(r){
    setTimeout(async function(){
      await cb();
    }, delay);

async function handleEvent(e) {
  const res = new Response('wait for it: ' + inMemoryCache);
  postProduction(e, async () => inMemoryCache += 1, 4000);
  return res;

addEventListener('fetch', e => e.respondWith(handleEvent(e)));

What is actual behavior? And what is guaranteed behavior?

Possible scenarios that I see are:

  1. there is no explicit time limit, but the setTimeouts are cancelled when the worker runtime decides to take the worker down. (If I get no response, my guess is that this is the workers behavior.)
  2. there is no explicit time limit, and the setTimeouts are forced to run immediately/ahead of schedule when the worker is taken down. If this is the case however, this would essentially enable an end-of-life callback/unload event for worker instances.
  3. there is a time limit, that is not visible.