From the through2 documentation:
Do you need this?
Since Node.js introduced Simplified Stream Construction, many uses of through2 have become redundant. Consider whether you really need to use through2 or just want to use the 'readable-stream' package, or the core 'stream' package (which is derived from 'readable-stream').
If I understand correctly, now (from 2021) we can intervene to the streams without third-party libraries.
I did not found how to do same thing as through2
in Stream documentation.
// ...
.pipe(through2(function (file, encoding, callback) {
// Do something with file ...
callback(null, file)
}))
// ↑ Possible to reach the same effect natively (with core packages)?
I suppose, for 2021 there must be some method supporting async/await syntax:
// ...
.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)?
What you are looking for is likely a Transform stream, something implemented by the native 'stream' library included with Node.js. I don't know that there is an async compatible version yet, but there is most definitely a callback based one. You need to inherit from the native Transform stream and implement your function.
Here's the boilerplate I like to use:
Now you can use this in places you would use through: