读取时出错
【Errors while reading】
在处理 readable._read() 时发生的错误必须通过 readable.destroy(err) 方法传播。从 readable._read() 内部抛出 Error 或手动触发 'error' 事件会导致未定义行为。
【Errors occurring during processing of the readable._read() must be
propagated through the readable.destroy(err) method.
Throwing an Error from within readable._read() or manually emitting an
'error' event results in undefined behavior.】
const { Readable } = require('node:stream');
const myReadable = new Readable({
read(size) {
const err = checkSomeErrorCondition();
if (err) {
this.destroy(err);
} else {
// Do some work.
}
},
});