Node.js/stream:修订间差异
来自康健生活
| 第170行: | 第170行: | ||
=== 流的复用 === | === 流的复用 === | ||
将一个流进行复用,留一旦被使用就会被消耗,但我们可以将流通过管道传输两次进行使用。 | |||
{{leftrightflex|<syntaxhighlight lang="javascript"> | |||
const spawn = require('child_process').spawn; | |||
const PassThrough = require('stream').PassThrough; | |||
const bar = new PassThrough(); | |||
const biz = new PassThrough(); | |||
const foo = spawn('echo', ['kangkk']); | |||
foo | |||
.stdout.on('end', function() { | |||
console.log('foo end'); | |||
}) | |||
.pipe(bar) | |||
.on('end', function() { | |||
console.log('bar end'); | |||
}) | |||
.pipe(biz) | |||
.on('end', function() { | |||
console.log('biz end'); | |||
});</syntaxhighlight>|{{consoleout|<pre> | |||
foo end | |||
bar end | |||
</pre>}}}} | |||
{{leftrightflex|<syntaxhighlight lang="javascript"> | {{leftrightflex|<syntaxhighlight lang="javascript"> | ||
const Readable = require('stream').Readable; | const Readable = require('stream').Readable; | ||