CKTIS / JavaScript
Asynchrony, concurrency, and suppressing stale results
Asynchronous operations can remain in progress together and finish in a different order from their start order. A generation token protects shared state from an older operation's result, but does not cancel its work.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
const token = (generation = Symbol()); const value = await operation(); if (token !== generation) return; commit(value);Asynchrony allows an operation's continuation to be deferred; concurrency means overlapping operation lifetimes, not necessarily parallel instruction execution. await suspends that async function, not the entire JavaScript agent. Other code can change shared state during the suspension.
Generation tokens to suppress stale async results implement “latest-started request wins”. Before the first await, create a fresh Symbol(), store it as the current generation, and retain it locally as token. After waiting, compare tokens immediately before committing, with no further await between the check and the write. An older result is ignored even while the newer operation is still pending.
Scope generation to the state being protected: independent views need independent tokens. Apply the same freshness check before displaying an error or changing loading state in finally. A token neither cancels work nor reverses its side effects. The example deliberately ignores stale errors and rethrows current errors; callers must handle those rejections.
The example forces the older operation to finish after the newer one, so the expected output is new. It uses ES2015 Promise and Symbol, plus ES2017 async/await. It requires no Web APIs; working in Node 24 does not establish browser API support.
Examples
(async () => {
let generation;
let visible = "empty";
async function update(result) {
const token = (generation = Symbol());
try {
const value = await result;
if (token !== generation) return;
visible = value;
} catch (error) {
if (token !== generation) return;
throw error;
}
}
let finishOld;
const oldResult = new Promise(resolve => {
finishOld = resolve;
});
const oldTask = update(oldResult);
const newTask = update(Promise.resolve("new"));
await newTask;
finishOld("old");
await oldTask;
console.log(visible);
})();Result
new
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.