Re: [w3c/ServiceWorker] event.waitUntil/respondWith vs return promise (#1148)

> If you passed sleep(Infinity) the browser would intervene.

Yep, and it could know which events it terminated early, which wouldn't be the case if `sleep(Infinity)` wasn't passed.

> a _developer_ will never not want to `waitUntil`

It's pretty common to not `waitUntil` in a fetch event.

> It might be useful to create a gist to demonstrate if there's a certain task/pattern that you think this wouldn't work for?

```js
addEventListener('install', event => {
  event.waitUntil(doAsyncStuff());
});
```

In the above example, if the promise returned by `doAsyncStuff()` rejects, the service worker does not install.

```js
addEventListener('fetch', event => {
  event.respondWith(
    fetch(whatever).then(response => {
      event.waitUntil(addThisToTheCache(response));
      return response;
    })
  );
});
```

In the above example, the developer declares they want to handle this request by calling `event.respondWith` (it isn't handled by the service worker otherwise). The promise passed to `respondWith` should resolve with a response. `waitUntil` is used to further extend the event to carry out work that needs to happen beyond the response of the request.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/ServiceWorker/issues/1148#issuecomment-306535001

Received on Tuesday, 6 June 2017 16:06:42 UTC