Future cancellation

Domenic Denicola domenic at domenicdenicola.com
Wed May 1 11:23:24 PDT 2013


From: Tab Atkins Jr. [jackalmage at gmail.com]

> I think you're making this far too complicated.  It's much simpler than this:

I disagree. An abstraction boundary gets broken. Consider:

```js
function getUser1() {
  return doXHR("/user.json");
}

function getUser2() {
  return { "name": "domenic" };
}

function getUser3() {
  return doXHR("/user.json").then(function (user) {
    user.metadata = "<meta> tags are old school";
  });
}
```

Here, `getUser1()` returns a cancellable promise, but `getUser2()` does not, even though they should have the same semantics. Worse, `getUser3()` isn't cancellable, even though it was originally derived from an XHR. Trying to fix the `getUser3()` case is what takes you down the slope of complex additional semantics.

Much better would be if a hypothetical future XHR utility returned a `{ promise, abort }` object, with no logical connection between the `abort` and the `promise` (besides maybe rejecting the promise with a well-known error). This seems much better than trying to make a general cancellation concept for promises and overloading them with additional semantics about the operation being performed.


More information about the es-discuss mailing list