W3C home > Mailing lists > Public > public-script-coord@w3.org > January to March 2015

RE: Cancellation architectural observations

From: Domenic Denicola <d@domenic.me>
Date: Mon, 2 Mar 2015 23:36:06 +0000
To: Tab Atkins Jr. <jackalmage@gmail.com>, Dean Tribble <tribble@e-dean.com>
CC: "public-script-coord@w3.org" <public-script-coord@w3.org>, es-discuss <es-discuss@mozilla.org>
Message-ID: <CY1PR0501MB136973193025D97178A09A84DF100@CY1PR0501MB1369.namprd05.prod.outlook.com>
From: es-discuss [mailto:es-discuss-bounces@mozilla.org] On Behalf Of Tab Atkins Jr.

> Cancellations should "chain"

This is the most important issue, in my mind. I often illustrate it with

```js
function fetchUser(id) {
  return fetch(`/users/user/${id}`);
}

function fetchUserAsJSON(id) {
  return fetchUser(id).then(JSON.parse);
}
```

If `fetchUser(1)` is cancellable but `fetchUserAsJSON(1)` is not, then I think we've failed.

However, I think this still works with cancellation tokens:

```js
function fetchUser(id, canceller) {
  return fetch(`/users/user/${id}`, { canceller });
}

function fetchUserAsJSON(id, canceller) {
  return fetchUser(id, canceller).then(JSON.parse);
}
```

The API stays the same for both of them. 

> Combinators should combine cancellations

Still works, I think, although again in a different form:

```js
function fetchUsers(ids, canceller) {
  return Promise.all(ids.map(id => fetchUser(id, canceller)));
}
```

> You need to be able to "clean" a cancellable promise

Simple:

```js
function fetchUserUncancellable(id) {
  return fetchUser(id); // no second argument
}
```

Overall I am more optimistic about cancellation tokens these days...
Received on Monday, 2 March 2015 23:36:35 UTC

This archive was generated by hypermail 2.3.1 : Monday, 2 March 2015 23:36:35 UTC