Experiments · JavaScript: From Closures to Autonomous Systems · Chapter 12

A timeout that cancels nothing

Cancelling a promise with AbortController and AbortSignal.timeout

Theme
Try itA timeout that cancels nothing
A slow operation writes a record to the database after half a second, and the caller will wait for it 100 ms at most. Run both versions on real timers: with Promise.race the client is told it failed and the record appears anyway; with AbortSignal the operation stops itself.

Everything is computed in your browser; nothing is sent anywhere.

About this experiment

Does Promise.race cancel the losing promise?

No. Promise.race settles with the first promise to settle and ignores the others, but it has no way to stop them. A promise only stands for a result; the work behind it, a timer, a network request or a database write, goes on until it finishes. To stop work, the code doing it has to accept a cancellation signal. In JavaScript that is the AbortSignal of an AbortController, or AbortSignal.timeout(ms), which aborts by itself after the given time. The function has to listen for the abort event or check signal.aborted, and clean up after itself.

The most common way to give an asynchronous call a deadline is Promise.race against a timer. The caller stops waiting when the timer wins, and the operation it was waiting for carries on, because nothing told it to stop. The experiment has a slow operation that writes a record to a database after 500 ms and a caller that will wait 100 ms at most.

Press “Run” on the Promise.race tab. The timeline fills in on the browser’s real timers: the caller gets its error at about 100 ms, and the operation keeps going and writes its record at about 500 ms. Press it a few more times, like a client who was told it failed, and watch the record count grow while every answer says “it didn’t work”. On the AbortSignal tab the operation receives AbortSignal.timeout(100), clears its own timer and writes nothing.

In the book, in Node 24, the caller got “timeout” at 102 ms and the record was written at 501 ms anyway. With AbortSignal.timeout(100) the operation stopped at 103 ms. Your browser’s numbers will be a few milliseconds off, depending on the engine and on how busy the tab is.

Once async/await made asynchronous code readable, the chapter says, three questions about time were left, and this is the first: how to stop work that is no longer needed. Cancellation came from the browser, as AbortController in the DOM standard, and Node took the same design. It works only if the signal is passed by hand into every function down the chain and every function checks it.

From the book

JavaScript: From Closures to Autonomous Systems · Chapter 12

Time as Part of the Program

Next to this experiment in the chapter
102 ms caller got an error: timeout, the client was told "failed" 501 ms the operation wrote the record to the database The client was told that nothing worked, and four hundred milliseconds later the record appeared after all. The client will most likely press the button again, and the record will appear a second time. Promise.race stopped waiting, but nobody stopped the operation itself. In the second variant the operation receives the signal AbortSignal.timeout(100) and at the hundred-and-third millisecond stops working on its own, so nothing reaches the database.