Skip to main content

Command cancellation

Deno Sandboxes provide a sandboxed environment for evaluating JavaScript code. This is useful for evaluating code that is not trusted or for testing code that is not safe to run in the main runtime.

You can cancel commands in a sandbox using the KillController class.

import { KillController, Sandbox } from "@deno/sandbox";

await using sandbox = await Sandbox.create();

// Start a long-running command
const controller = new KillController();
const cmd = sandbox.sh`sleep 30`.signal(controller.signal);
const promise = cmd.text();

// Cancel after 2 seconds
setTimeout(() => {
  controller.kill(); // Kill the process
}, 2000);

try {
  await promise;
} catch (error) {
  console.log("Command was cancelled:", error);
}

For more information about Sandboxes, see the Sandboxes documentation.

Did you find what you needed?

Privacy policy