nodejs 文档对此问题做了如下说明:
It is unsafe to use fs.write() multiple times on the same file without waiting for the callback. For this scenario, fs.createWriteStream() is recommended.
现在,我在 deno 文档中找不到任何有关它的信息,我想知道 deno 是否还建议不要在同一个文件上进行多次写入,或者他们是否已经处理过Rust 的情况有所不同。 如果他们提出反对意见,建议的解决方法是什么?
谢谢!
最佳答案
不等待 Promise 完成的异步 write
对于 deno 来说也是不安全的。
实际演示:
// If safe the file content would be ABCDEFGHIJ
async function main() {
let n = new Uint8Array(1);
const file = await Deno.open("bar.txt", { write: true, create: true });
for (var i = 65; i < 75; i++) {
n[0] = i;
Deno.write(file.rid, n);
}
}
main();
在我的 Linux 盒子上 bar.txt
内容不是 ABCDEFGHIJ
而是“JJ”。
请注意,这个特定示例实际上显示了两个问题:
- 在没有
await
的情况下,无法使用Deno.write
安全访问文件资源 - 在
write()
调用之间共享相同的最后一个i
循环变量值J
。
关于javascript - 我可以在 deno 中异步多次写入文件吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60809373/