Concatenate two (or n) streams

前端 未结 10 1262
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-24 02:34
  • 2 streams:

    Given readable streams stream1 and stream2, what\'s an idiomatic (concise) way to get a stream containing

10条回答
  •  情话喂你
    2020-12-24 02:53

    If you don't care about the ordering of data in the streams, a simple reduce operation should be fine in nodejs!

    const {PassThrough} = require('stream')
    
    let joined = [s0, s1, s2, ...sN].reduce((pt, s, i, a) => {
      s.pipe(pt, {end: false})
      s.once('end', () => a.every(s => s.ended) && pt.emit('end'))
      return pt
    }, new PassThrough())
    

    Cheers ;)

提交回复
热议问题