Skip to main content

命令取消

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.

你可以使用 KillController 类在沙箱中取消命令。

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

await using sandbox = await Sandbox.create();

// 启动一个长时间运行的命令
const controller = new KillController();
const cmd = sandbox.sh`sleep 30`.signal(controller.signal);
const promise = cmd.text();

// 2 秒后取消
setTimeout(() => {
  controller.kill(); // 终止进程
}, 2000);

try {
  await promise;
} catch (error) {
  console.log("命令已被取消:", error);
}

For more information about Sandboxes, see the Sandboxes documentation.

你找到了你需要的东西吗?

编辑此页面
隐私政策