我正在尝试找出 Node 中的流并尝试 the stream handbook 中的一些示例
我正在尝试 _read可读流的方法。它说,它接受一个参数,该参数是消费者想要读取的字节数。
我有两个问题。
- 是消费者想要读取“水印”的字节数
- 为什么当我将 _read 与参数一起使用时会出现错误。
这是我的代码。
var Readable = require('stream').Readable;
var rs = Readable();
var c = 97;
rs._read = function (5) {
rs.push(String.fromCharCode(c++));
if (c > 'z'.charCodeAt(0)) {
rs.push('\n');
rs.push(null);
}
};
setTimeout(function () {
rs.pipe(process.stdout);
}, 2000);
这就是错误
/Users/nikhilkuria/Dev/git/node_demo/streams/streamRead.js:5
rs._read = function (5) {
^
SyntaxError: Unexpected number
at Object.exports.runInThisContext (vm.js:76:16)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:394:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:509:3
最佳答案
function (5) {
不是创建函数的有效语法。您需要传递变量名称而不是数字,如下所示:function (a) {
您看到的错误是由于 JavaScript 不允许变量名称以数字开头或仅由数字组成。
关于node.js - 可读流 Node : the _read method with the Number of bytes param,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42885950/