Skip to content

ExamplesDeduplication & Abort

Deduplication & Abort

Identical reads in flight share one request, a newer call cancels the one it replaces, and any call can be aborted.

both modes

Identical reads share one request

Three identical reads start together. The browser counts the requests that reached the proxy.

ts
// Three components read the same list at the same moment
const [header, sidebar, page] = await Promise.all([
  caller.users.getAll(),
  caller.users.getAll(),
  caller.users.getAll(),
])

// One request reached the proxy; each call got a copy of its own
Promise.all([users.getAll(), users.getAll(), users.getAll()])SANDBOX
idle
// run the request to see the response
Deduplication docs

Cancel the previous call

The endpoint answers after 3 s. Call again before it does: the older call is cancelled with status 499, and the newer one is answered by the request already in flight.

ts
import { createProxyCaller } from "@noverax/courier"
import { apiRoutes } from "@/courier.routes"

const searchCaller = createProxyCaller(apiRoutes, {
  cancelPrevious: ["misc.slow"],
})

// A newer call cancels the one still in flight: it rejects with status 499
await searchCaller.misc.slow()
searchCaller.misc.slow()SANDBOX

calls 0in flight 0cancelled 0

idle
// run the request to see the response
cancelPrevious docs

Manual abort

Start a slow request (3s delay) and cancel it before it completes.

ts
const controller = new AbortController()

// Pass signal via .with() to cancel the request at any time
const promise = caller.misc.with({ signal: controller.signal }).slow()

// Cancel the request
controller.abort()
caller.misc.with({ signal }).slow()SANDBOX
idle
// run the request to see the response
Abort docs