Future cancellation

Ron Buckton rbuckton at chronicles.org
Wed May 1 12:03:34 PDT 2013


This is where something like an external cancellation source could be more effective:

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

function getUser2(cancelToken) {
  // immediate result, no need for cancellation
  return { "name": "domenic" };
}

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

var cts = new CancellationTokenSource();
var usersF = Future.every(getUser1(cts.token), getUser2(cts.token), getUser3(cts.token));
usersF.done(function(users) {
});
cts.cancelAfter(5000); // timeout after 5 seconds.
```

Rather than subclassing Future, you use an external source provided by the caller to manage cancellation. Now it doesn't matter whether either of the 3 methods have a return value that supports cancellation, and if the caller doesn't need to cancel, it can provide null or undefined.

Ron

> -----Original Message-----
> From: es-discuss-bounces at mozilla.org [mailto:es-discuss-
> bounces at mozilla.org] On Behalf Of Domenic Denicola
> Sent: Wednesday, May 1, 2013 11:23 AM
> To: Tab Atkins Jr.; Bill Frantz
> Cc: public-script-coord at w3.org; Brendan Eich; es-discuss
> Subject: RE: Future cancellation
> 
> 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.
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss




More information about the es-discuss mailing list