/deno-async-signal

Handling process signals asynchronously in Deno.

Primary LanguageTypeScriptMIT LicenseMIT

async-signal

license:MIT jsr Test codecov

async-signal provides functions for handling process signals asynchronously in Deno.

Deno limitation: On Windows only "SIGINT" (Ctrl+C) and "SIGBREAK" (Ctrl+Break) are supported.

import { asyncSignal, SignalError } from "@milly/async-signal";
import { delay } from "@std/async";

try {
  // When the block exits, the signal trap is removed.
  using intTrap = asyncSignal("SIGINT");

  // It resolves after 5 seconds or rejecets if Ctrl+C is pressed.
  await Promise.race([intTrap, delay(5000)]);
} catch (err) {
  // `intTrap` rejecets with `SignalError`.
  if (err instanceof SignalError) {
    console.error(`${err.signal} is trapped`);
  }
}