Re: [ServiceWorker] Allow respondWith() to be called asynchronously or allow some way to cancel the response. (#836)

Agree this should be middleware, since it might be a series or a race depending on the app, and you may not want to run additional handlers until others fail to provide a response.

```js
const handlers = [];

self.addEventListener('fetch', event => {
  event.respondWith(async function() {
    for (const handler of handers) {
      const response = await handler(event.request);
      if (response) return response;
    }
    return fetch(event.request);
  }());
});

handlers.push(request => {
  // …
});

handlers.push(request => {
  // …
});

handlers.push(request => {
  // …
});
```

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

Received on Monday, 22 February 2016 16:07:06 UTC