diff --git a/lib/internal/stream_base_commons.js b/lib/internal/stream_base_commons.js index f6c3c5a8cf1063..76a87ab74b74e6 100644 --- a/lib/internal/stream_base_commons.js +++ b/lib/internal/stream_base_commons.js @@ -83,20 +83,18 @@ function onWriteComplete(status) { const stream = this.handle[owner_symbol]; - if (stream.destroyed) { - if (typeof this.callback === 'function') - this.callback(null); - return; + if (status < 0) { + const error = new ErrnoException(status, 'write', this.error); + if (typeof this.callback === 'function') { + return this.callback(error); + } + + return stream.destroy(error); } - // TODO (ronag): This should be moved before if(stream.destroyed) - // in order to avoid swallowing error. - if (status < 0) { - const ex = new ErrnoException(status, 'write', this.error); + if (stream.destroyed) { if (typeof this.callback === 'function') - this.callback(ex); - else - stream.destroy(ex); + this.callback(null); return; }