Skip to content

Commit 4557b48

Browse files
committed
stream: 'error' should be emitted asynchronously
errorOrDestroy emits 'error' synchronously due to compat reasons. However, it should be possible to use correct async behaviour for new code.
1 parent 35bfe0e commit 4557b48

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

lib/_stream_writable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ Writable.prototype.write = function(chunk, encoding, cb) {
309309
} else if (state.destroyed) {
310310
const err = new ERR_STREAM_DESTROYED('write');
311311
process.nextTick(cb, err);
312-
errorOrDestroy(this, err);
312+
errorOrDestroy(this, err, true);
313313
} else if (isBuf || validChunk(this, state, chunk, cb)) {
314314
state.pendingcb++;
315315
ret = writeOrBuffer(this, state, isBuf, chunk, encoding, cb);

lib/internal/streams/destroy.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function undestroy() {
107107
}
108108
}
109109

110-
function errorOrDestroy(stream, err) {
110+
function errorOrDestroy(stream, err, tick) {
111111
// We have tests that rely on errors being emitted
112112
// in the same tick, so changing this is semver major.
113113
// For now when you opt-in to autoDestroy we allow
@@ -123,8 +123,13 @@ function errorOrDestroy(stream, err) {
123123

124124
if ((r && r.autoDestroy) || (w && w.autoDestroy))
125125
stream.destroy(err);
126-
else if (needError(stream, err))
127-
stream.emit('error', err);
126+
else if (needError(stream, err)) {
127+
if (tick) {
128+
process.nextTick(emitErrorNT, stream, err);
129+
} else {
130+
stream.emit('error', err);
131+
}
132+
}
128133
}
129134

130135

0 commit comments

Comments
 (0)