Defjs

Abort or time out a call and read NET_ABORTED / NET_TIMEOUT.

Cancel an HTTP call

Combine the caller's signal with a deadline on signal. AbortSignal.any([controller.signal, AbortSignal.timeout(5_000)]) is one signal. A manual abort classifies as NET_ABORTED. A deadline classifies as NET_TIMEOUT.

See HTTP for details.

import { createClient, defineRequest, withEndpoint } from "@defjs/core";

const client = createClient(withEndpoint("https://api.example.com"));
const getReport = defineRequest({ method: "GET", path: "/report" });

const controller = new AbortController();
const pending = getReport(
  client({
    signal: AbortSignal.any([controller.signal, AbortSignal.timeout(5_000)]),
  }),
);

controller.abort("screen closed");

async function main() {
  const [error] = await pending;
  if (error?.code === "NET_ABORTED") {
    console.log("caller cancelled");
    return;
  }
  if (error?.code === "NET_TIMEOUT") {
    console.log("timed out");
    return;
  }
  if (error) {
    console.error(error.code);
    return;
  }
  console.log("got the report");
}

void main();
caller cancelled

Cancellation tells you what the caller observed. It does not prove a server-side write rolled back — keep mutation retries behind an idempotency contract.

Edit this page on GitHub
Released under the MIT License. · Copyright © 2026 MunMunMiao

On this page