Re: [fetch] Aborting a fetch (#27)

There has been a lot of ink (ok, electrons) spilled on this topic already, but I don't think anyone's brought this up.

People claim that cancelling has no analogue in synchronous code, but I think it does: interrupts. When you interrupt a thread, you basically cause any blocking operation in that thread to fail, and that interrupt propagates through the call stack as appropriate. In the case of promises, if you cancel someone at the end of the chain, it causes the head of the chain to fail. In other words, in this code:

```javascript
let a = fetch(url);
let b = a.then(...);
let c = b.then(...);
let d = c.then(...);
d.then(v => console.log("Success"), e => console.log("Failure"));
d.cancel();
```
The most appropriate thing to do is to abort the URL fetch and have "Failure" get printed to the command line.

The problem of course is if two people tried to say ```a.then()```, which comes down to a question of if "cancel" means "kill" or "ignore." One could sidestep the issue by banning then on cancellable promises from being called more than once, but I'm not sure if that's still feasible. For simplicity, though, I do like the idea of CancellablePromise.cancel as being defined as "take the 'original' promise, call ```reject(ABORTED_PROMISE)``` and then ```cancel()``` for the values of ```reject``` and ```cancel``` provided in the promise constructor.

I prefer the use of a special well-known exception for cancelling instead of a promise method, since it makes it possible to approximate async functions with a wrapper around a generator, which is not possible with only a .finally method to catch cancellations.

---
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/fetch/issues/27#issuecomment-93858605

Received on Thursday, 16 April 2015 23:02:13 UTC