What's the difference between .pipe and pipeline on streams

前端 未结 3 1899
小蘑菇
小蘑菇 2021-01-12 07:57

I found two different ways to pipe streams in node.js

Well known .pipe() method of a stream

https://nodejs.org/api/stream.html#stream_readable_pip

3条回答
  •  半阙折子戏
    2021-01-12 08:25

    pipeline is the improved version of pipe it was added to stream module since Node.js v10

    Also, pipeline takes any number of arguments and the last argument is a callback used to know when the pipeline ends or throws an error.

    Usage pipe:

    mySourceStream.pipe(myStream).pipe(anotherStream)
    

    Usage pipeline:

    mySourceStream.pipeline(myStream, anotherStream, err => {
      if (err) {
        console.log('There is an error')
     } else {
        else console.log('pipeline successful')
     }
    })
    

提交回复
热议问题