RE: Cancellation architectural observations

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