cabinet:在 Deno 中用更优雅的方式处理文件

cabinet 是一个文件管理的包,我们可以在 Deno 代码中使用 cabinet 更优雅的管理文件。

使用方式

import {
  Cabinet,
  CabinetFile,
  CabinetError,
} from "https://deno.land/x/cabinet/mod.ts";

// 创建 Cabinet 类
const file = new Cabinet("./testing.txt");

// 向文件写入内容
file.write("The date is " + new Date());

file.write("The date is " + new Date(), () => console.log("finished!"));

file.writer.sync("This was written synchronously");

file.writer
  .promise("This was written with a promise")
  .then(() => console.log("finished!"));

file.writer.callback("This was written with a callback", () =>
  console.log("finished!")
);

file.read();

file.read(() => console.log("finished!"));

file.reader.sync();

file.reader.promise().then((cFile: CabinetFile) => {
  console.log("read " + cFile.size.mb + "mb in promise");
});

file.reader.callback((err?: CabinetError, cFile?: CabinetFile) => {
  console.log("read " + cFile?.size.mb + "mb in callback");
});
1 个赞

我觉得除了这个形参的定义,其他都很优雅

哈哈哈,cFile 吗?

当然是错误优先回调啊-54b1fe47d5931bbf