【发布时间】:2021-12-16 06:05:23
【问题描述】:
我的代码无法正常工作,因为在下一行执行之前文件尚未解压缩。 这是我的解压函数:
import { createGunzip } from 'zlib';
import { createReadStream, statSync, createWriteStream } from 'fs';
function fileExists(filePath: string) {
try {
return statSync(filePath).isFile();
} catch (err) {
return false;
}
}
async function gunzipFile(source: string, destination: string): Promise<void> {
if (!fileExists(source)) {
console.error(`the source: ${source} does not exist`);
return;
}
const src = createReadStream(source);
const dest = createWriteStream(destination);
await src.pipe(createGunzip())
.pipe(dest)
.on('error', (error) => {
// error logging
})
.on('end', () => {
return;
});
}
我将如何重构它以使其异步正常工作?或非异步,如果它会在完成之前等待。有类似的问题,但它们对我不起作用,可能是因为此函数返回 void,而不是已流式传输的数据。
【问题讨论】:
标签: typescript async-await fs data-stream