Re: [whatwg/fetch] Aborting a fetch: The Next Generation (#447)

The following also works with fetch:

```js
async function getResource(onCancel) {
  var controller = new FetchController(), 
  signal = controller.signal;
  onCancel(() => controller.abort());
  var result = await fetch("./someResource", {signal}).then(req => req.text());
  return process(result);
}

// outside
var cancel;
var resourcePromise = getResource(fn => cancel = fn);
onExternalEventThatCausesCancellation(() => {
  cancel(); // calls .abort on the fetch controller.
});

// Or:
var resource = await getResource(onExternalEventThatCausesCancellation);

```

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/fetch/issues/447#issuecomment-316219229

Received on Tuesday, 18 July 2017 22:43:22 UTC