Cancellation architectural observations

Domenic Denicola d at domenic.me
Mon Mar 2 23:36:06 UTC 2015


From: es-discuss [mailto:es-discuss-bounces at 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...


More information about the es-discuss mailing list