Re: [ServiceWorker] setTimeout/setInterval are unreliable in ServiceWorkers (#838)

Agree with @wanderview and @mkruisselbrink here, `setTimeout` is no more dangerous here than other async APIs. `waitUntil` is the mechanism we have to extend an event, if you don't use that, you get no guarantees.

Say I wanted to race the cache & the network, but prefer network content if it can arrive in 500ms:

```js
self.addEventListener('fetch', event => {
  event.respondWith(
    new Promise((resolve, reject) => {
      fetch(event.request).then(resolve, () => cachePromise);
      
      const cachePromise = caches.match(event.request)
        .then(response => response || caches.match('/error'));
      
      setTimeout(() => {
        resolve(cachePromise);
      }, 500);
    })
  );
});
```

Not sure how you'd achieve this without `setTimeout`.

---
Reply to this email directly or view it on GitHub:
https://github.com/slightlyoff/ServiceWorker/issues/838#issuecomment-188273853

Received on Wednesday, 24 February 2016 14:21:11 UTC