从through2文档中:
你需要这个吗?
自从 Node.js 引入了 Simplified Stream Construction 之后,through2 的很多用法就变得多余了。考虑您是否真的需要使用 through2 或只想使用“可读流”包或核心“流”包(派生自“可读流”)。
如果我理解正确,现在(从 2021 年开始)我们可以在没有第三方库的情况下干预流。我没有找到与Stream 文档through2
中相同的方法。
// ...
.pipe(through2(function (file, encoding, callback) {
// Do something with file ...
callback(null, file)
}))
// ↑ Possible to reach the same effect natively (with core packages)?
我想,对于 2021 年,一定有一些方法支持 async/await 语法:
// ...
.pipe(newFeatureOfModernNodeJS(async function (file) {
await doSomethingAsyncWithFile(file);
// on fail - same effect as "callback(new Error('...'))" of trough2
return file; // same effect as "callback(null, file)" of trough2
// or
return null; // same effect as `callback()` of trough2
}))
// ↑ Possible to reach the same effect natively (with core packages)?