This site counts visits with analytics cookies only if you accept. Read the consent

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.

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

Requests

as the browser recorded them and the site's server told of them

// no request yet: run the call

Deduplication docs

Cancel the previous call

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

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

const caller = createProxyCaller(routes.api)

// A newer call made the same way cancels the one still in flight, which rejects with an
// AbortError whose code is "SUPERSEDED"
await caller.misc.with({ cancelPrevious: true }).slow()

// Or once for every call of the endpoint, where it is defined:
// slow: endpoint.get("slow").cancelPrevious()
caller.misc.with({ cancelPrevious: true }).slow()SANDBOX

calls 0in flight 0cancelled 0

idle
// run the request to see the response

Requests

as the browser recorded them and the site's server told of them

// no request yet: run the call

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

Cancelled, the request gets no answer, so the site's server has none to tell its side in: it stops its own request to the API as the browser leaves. Chrome records the cancelled request below; Safari records none. Your browser's developer tools show it under Network as cancelled.

Requests

as the browser recorded them and the site's server told of them

// no request yet: run the call

Abort docs