读取时出错
¥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.
}
},
});