如何在 NodeJS / TypeScript 中使用 Promises 获取文件大小

首先,从 NodeJS 标准库导入 stat

import_stat.ts
import { stat } from "node:fs/promises";

Async-await 风格

filesize_async.ts
// Async-await 风格:
const statResult = await stat("myfile.pdf");
const fileSizeInBytes = statResult.size;

Promise.then() 风格

filesize_then.ts
stat("myfile.pdf").then(statResult => {
    const fileSizeInBytes = statResult.size;
    // TODO 你的代码放在这里
});

Check out similar posts by category: NodeJS, Typescript