DEP0137:在垃圾回收时关闭 fs.FileHandle
【DEP0137: Closing fs.FileHandle on garbage collection】
类型:寿命终止
【Type: End-of-Life】
以前允许在垃圾回收时关闭 fs.FileHandle 对象,但现在会抛出错误。
【Allowing a fs.FileHandle object to be closed on garbage collection used
to be allowed, but now throws an error.】
请确保在不再需要 fs.FileHandle 时,使用 FileHandle.prototype.close() 显式关闭所有 fs.FileHandle 对象:
【Please ensure that all fs.FileHandle objects are explicitly closed using
FileHandle.prototype.close() when the fs.FileHandle is no longer needed:】
const fsPromises = require('node:fs').promises;
async function openAndClose() {
let filehandle;
try {
filehandle = await fsPromises.open('thefile.txt', 'r');
} finally {
if (filehandle !== undefined)
await filehandle.close();
}
}