DEP0209:使用 AbortSignal 来释放资源
🌐 DEP0209: Using AbortSignal to dispose of resources
类型:仅文档
🌐 Type: Documentation-only
使用 AbortSignal 来销毁长期存在的资源已不推荐。建议使用 using 来清理资源。
🌐 Using AbortSignal to destroy long-lived resources is deprecated. Prefer
using for resource cleanup.
AbortSignal 仍然很适合用于取消操作、从外部传播取消以及超时处理。
// Deprecated
async function example() {
const ac = new AbortController();
const server = http.createServer(handler);
server.listen({ port: 3000, signal: ac.signal });
await doWork();
ac.abort();
} // Use this instead
async function example() {
await using server = http.createServer(handler);
server.listen(3000);
await doWork();
} // Deprecated
async function example() {
const ac = new AbortController();
const stream = addAbortSignal(ac.signal, fs.createReadStream(file));
await consume(stream);
ac.abort();
} // Use this instead
async function example() {
await using stream = fs.createReadStream(file);
await consume(stream);
} // Deprecated
async function example() {
const ac = new AbortController();
const child = spawn(command, args, { signal: ac.signal });
await doWork();
ac.abort();
} // Use this instead
async function example() {
using child = spawn(command, args);
await doWork();
}