Paged search: competing requests and cancellation
Project milestone
Protect the current query from older requests by combining cancellation with a monotonically increasing request version. Cancellation alone cannot guarantee that a transport stops.
Run the examples
In projects/paged-search/, save the snippet as example.cjs and run node example.cjs in a fresh process.
const {createSearch}=require('./search.cjs');(async()=>{const pending=[];const s=createSearch(()=>new Promise(r=>pending.push(r)));const old=s.start('old'), fresh=s.start('fresh');pending[1]({items:[],next:null});await fresh;pending[0]({items:[],next:null});console.log(await old,s.snapshot().query);})();Expected console output:
false freshThe obsolete version cannot overwrite the new query.
In projects/paged-search/, save the snippet as example.cjs and run node example.cjs in a fresh process.
const {createSearch}=require('./search.cjs');(async()=>{const s=createSearch(async()=>({items:[],next:null}));await s.start('pen');const copy=s.snapshot();copy.query='changed';console.log(s.snapshot().query);})();Expected console output:
penThe returned state is cloned.
Implementation and contract
createSearch() aborts the previous controller before starting a request and captures its own version. After await, only the current version can publish state. An injected transport may ignore the signal; the version check still suppresses stale outcomes. A snapshot returns a clone, so callers cannot mutate internal state.
Common mistakes
Never use completion order as the authority for which query is current. Protect failure paths as well as successful paths.
Acceptance task
Write a regression test where the first request rejects after the second succeeds. The final phase must stay ready.
Project source, setup and tests: projects/paged-search/README.md. Read the implementation before modifying it; the tests check the supplied baseline, not every possible implementation.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Paged search: competing requests and cancellation — Knowledge Check
Assess implementation decisions and project behavior.
Why retain a request version after calling abort?
Which outcome must be checked against the current version?
What should a snapshot caller be able to do?
In the Paged search: competing requests and cancellation lab, what does this independent example log? const {createSearch}=require('./search.cjs'); (async()=>{const pending=[];const s=createSearch(()=>new Promise(r=>pending.push(r))); const old=s.start('old'), fresh=s.start('fresh'); pending[1]({items:[],next:null});await fresh; pending[0]({items:[],next:null});console.log(await old,s.snapshot().query);})();
In the Paged search: competing requests and cancellation lab, what does this independent example log? const {createSearch}=require('./search.cjs'); (async()=>{const s=createSearch(async()=>({items:[],next:null}));await s.start('pen');const copy=s.snapshot();copy.query='changed';console.log(s.snapshot().query);})();