为什么以下代码(保存为dummy.js
)会抛出错误?有什么问题?
var Stream = require('stream');
var src = new Stream.Readable();
var dest = new Stream();
dest.writable = true;
dest.write = function(data) { console.log("dest:",data); };
["1","2","3"].forEach(function(f) { console.log(f); src.push(f); });
src.pipe(dest);
执行:
> node dummy.js
1
2
3
events.js:72
throw er; // Unhandled 'error' event
^
Error: not implemented
at Readable._read (_stream_readable.js:446:22)
at Readable.read (_stream_readable.js:320:10)
at Readable.on (_stream_readable.js:701:14)
at Readable.pipe (_stream_readable.js:556:10)
at Object.<anonymous> (/home/jay/dummy.js:16:5)
at (etc.)
答案 0 :(得分:2)
在推送数组值之后和调用null
之前推送pipe
。还需要在dest.end = function(data) {};
之后添加data.write = ...
。