TYPESCRIPT ASYNC QUEUE

Processing Data Asynchronously but Sequentially

Category Theory is an abstract, but sometimes disdained field of mathematics and computer science. It’s not uncommon to hear a developer make a statement like “Category Theory has something to do with functional programming, but I don’t really understand it.”

I’ve used a concept from Category Theory, the “Kleisli arrow”, to build a small library that solves a problem that (at least in JavaScript) is surprisingly difficult. The technical details are in the README file in the GitHub repo here.

The problem: Imagine you are selling something that is in high demand and short supply. For example, tickets to a big concert can sell out within minutes of being made available. These orders cannot be processed in parallel or in random order, since the first people to log in need to have their orders processed first. It also needs to be done asynchronously so that the server doesn’t become unresponsive for minutes or hours.

The library uses an asynchronous queue that can process any number of tasks sequentially, but (crucially) where the number of tasks is not known ahead of time. In fact, tasks can still be added to the queue after the first task has started.

One use case would be a website rapidly processing thousands or millions of forms submitted via a website. (For example, the ticket sales mentioned above). The app is a simplified demonstration of that. The form consists of two fields: The number to add to a running total, and the amount of time to wait before adding it. (The delay is to prove that the webpage remains responsive while the task is processing).

The biggest challenge is to handle cancellations cleanly. In JavaScript, Promises cannot be stopped once started (or once added to a chain using the .then function). The app has buttons to test some of these tricky use cases.

CONTACT

If you’re interested in discussing how I can help your project or business, please click the button below and send me a message