Re: [ServiceWorker] Returning a FetchPromise from fetch() (#625)

It's worth mentioning an alternate approach which I think avoids the ambient authority problems. @bterlson brought it up when I talked to him about this. It is the cancellation token idea.

A cancellation token basically has three things: `requestCancel()`, `isCancelled`, and `setCancelHandler()`. The consumer is meant to create one, give it to the producer, and later call `requestCancel()`. `requestCancel()` will set `isCancelled` to true and execute any callback set by `setCancelHandler()`. Then the producer can periodically check `isCancelled` at break points during their work, or call `setCancelHandler()` to get a callback executed when `requestCancel()` is called. So for fetch this would probably look something like

```js
const token = new CancellationToken();
fetch("http://example.com", token);

// later
token.requestCancel();
```

There's a slight modification to this design that re-uses the promise mechanism. E.g. if CancellationToken is actually `{ requestCancel(), cancelled }` where `cancelled` is a promise and `requestCancel()` is the promise's corresponding resolve function, then things are basically equivalent, but with less moving parts and no possibility for synchronous cancellation (which is probably fine?).

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

Received on Saturday, 21 February 2015 00:10:26 UTC