property
Spawn.BaseOptions.signal
signal?: AbortSignal
An AbortSignal that kills the subprocess when aborted.
Use this to abort the subprocess when another part of the program is aborted, such as a fetch.
If the signal is already aborted when spawn is called, no process is created and an AbortError (with cause set to signal.reason) is thrown synchronously.
If the signal is aborted after the process starts, the process is killed with the signal specified by killSignal (defaults to SIGTERM).
const controller = new AbortController();
const { signal } = controller;
const start = performance.now();
const subprocess = Bun.spawn({
cmd: ["sleep", "100"],
signal,
});
await Bun.sleep(1);
controller.abort();
await subprocess.exited;
const end = performance.now();
console.log(end - start); // 1ms instead of 101ms