例如,我有这个:
var r = fs.createReadStream('file.txt');
var z = zlib.createGzip();
var w = fs.createWriteStream('file.txt.gz');
r.pipe(z).pipe(w);
我想在r.pipe(z).pipe(w)完成后做些事情.我尝试过这样的事情:
var r = A.pipe(B);
r.on('end', function () { ... });
但不适用于管道链.
我该如何运作?
解决方法:
您正在寻找结束事件.从docs开始:
When the end() method has been called, and all data has been flushed to the underlying system, this event is emitted.
当源流发出一个end时,它将在目标(在您的情况下为B)上调用end().因此,您可以执行以下操作:
var r = A.pipe(B);
r.on('finish', function () { ... });