A discussion about cancellation and promises with some first-hand prospectives, might be interesting to those who track TC39’s “ECMAScript Cancellation” proposal and live in both JavaScript and .NET ecosystems.

Here’s my take.

The proper cancellation logic of pending asynchronous operations is an important consideration while designing the front-end, and it can also improve the scalability of the back-end.

For example, consider Autocomplete:

  • user types a character, we start an async delay (e.g., via setTimeout);

  • when they delay is completed, we start an async fetch that returns a promise;

  • meanwhile user is typing another character, while we still haven’t received the server’s response for the previous fetch;

  • ideally, we now need to cancel the fetch (or the delay, whatever is pending), before we start a new sequence of async calls. E.g., see the delayWithCancellation from the title link;

  • on the server, we most likely also want to cancel the pending API call to another micro-service that we use for auto-completion;

  • now we can start another delay and the fetch, and so on.

Thus, cancellation is pervasive through all code layers, similar to async/await itself.

Yet, currently there is no standard framework for cancellation in JavaScript. It’s been a bumpy road, check Ben Lesh’s “Promise Cancellation Is Dead — Long Live Promise Cancellation!” for some insights.

The current TC39 proposal is modelled after Cancellation in Managed Threads, the approach taken by the .NET ecosystem. It can very well be applied to asynchronous JavaScript as well, albeit JavaScript is a single-threaded language.

In a nutshell, it deals with two concepts:

  • the Cancellation Source. This is the “producer” part of the API, the code which triggers cancellation and it’s normally external to the asynchronous operation itself.

  • the Cancellation Token. This is the “consumer” part of the API, that lets the code which conducts the async operation itself to observe the request to cancel it and act properly. Think of addEventListener for the hypothetical cancel event. Back to the Autocomplete example, we may want to call clearTimer for the delay, or AbortController.abort for the HTTP fetch.

Hope this makes sense. There are many ways to tackle this, and there have been many takes at implementing cancellation in JavaScript and to mary it with Promises. One notable example is Bluebird Library.

The GitHub issue I referenced here is just yet another attempt at that, my own modest take with custom CancellablePromise class that extends the standard Promise and uses Rob’s Prex library for CancellationTokenSource and CancellationToken. It’s been great to get some feedback from Rob, and I hope the discussion will continue.

For a complete example, here’s a RunKit.

Author: admin

Leave a Reply

Your email address will not be published.