我很困惑使用管道来处理写入流是否同步,因为我发现了一个关于回调处理管道完成的问题
我只是想确保在做其他人之前完成写入流,比如fs.rename
,所以我承诺它,代码如下:
(async function () {
await promiseTempStream({oldPath, makeRegex, replaceFn, replaceObj, tempPath})
await rename(tempPath, oldPath)
function promiseTempStream({oldPath, makeRegex, replaceFn, replaceObj, tempPath}) {
return new Promise((res, rej) => {
const writable = fs.createWriteStream(tempPath)
fs.createReadStream(oldPath, 'utf8')
.pipe(replaceStream(makeRegex ,replaceFn.bind(this, replaceObj), {maxMatchLen: 5000}))
.pipe(writable)
writable
.on('error', (err) => {rej(err)})
.on('finish', res)
})
}
}())
它有效,但在阅读管道文档后我很困惑,因为它说
默认情况下,当源可读流发出"end"时,在目标可写流上调用 stream.end(),以便目标不再可写。
所以我只需要
await fs.createReadStream(oldPath, 'utf8')
.pipe(replaceStream(makeRegex ,replaceFn.bind(this, replaceObj), {maxMatchLen: 5000}))
.pipe(fs.createWriteStream(tempPath))
await rename(tempPath, oldPath)
或者只是
fs.createReadStream(oldPath, 'utf8')
.pipe(replaceStream(makeRegex ,replaceFn.bind(this, replaceObj), {maxMatchLen: 5000}))
.pipe(fs.createWriteStream(tempPath))
await rename(tempPath, oldPath)
哪种方法是正确的?非常感谢
您需要等待 tempPath 流上的 finish
事件。所以你可以做一些类似的事情
async function createTheFile() {
return new Promise<void>(resolve => {
let a = replaceStream(makeRegex, replaceFn.bind(this, replaceObj), { maxMatchLen: 5000 });
let b = fs.createWriteStream(tempPath);
fs.createReadStream(oldPath, 'utf8').pipe(a).pipe(b);
b.on('finish', resolve);
}
}
await createTheFile();
rename(tempPath, oldPath);
基本上在这里,我们创建了一个承诺,当我们完成写入 tempFile 时,该承诺会解决。在继续之前,您需要等待这一承诺。
但是,如果您还使用流添加一些错误处理代码,如 node.js 流的错误处理中所述,那就太好了