diff --git a/doc/api/net.md b/doc/api/net.md index 6eb661b43af90c..86c9159bcfbcab 100644 --- a/doc/api/net.md +++ b/doc/api/net.md @@ -643,6 +643,8 @@ callback. added: v0.1.90 --> +* Returns: {net.Socket} + Ensures that no more I/O activity happens on this socket. Only necessary in case of errors (parse error or so). diff --git a/doc/api/stream.md b/doc/api/stream.md index ca37243c90e62d..ff1b0c78fbcc29 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -515,8 +515,10 @@ A Writable stream in object mode will always ignore the `encoding` argument. added: v8.0.0 --> +* Returns: `this` + Destroy the stream, and emit the passed error. After this call, the -writible stream has ended. Implementors should not override this method, +writable stream has ended. Implementors should not override this method, but instead implement [`writable._destroy`][writable-_destroy]. ### Readable Streams diff --git a/lib/internal/streams/destroy.js b/lib/internal/streams/destroy.js index 37e7d4bc364299..86a22633f2fe78 100644 --- a/lib/internal/streams/destroy.js +++ b/lib/internal/streams/destroy.js @@ -14,7 +14,7 @@ function destroy(err, cb) { (!this._writableState || !this._writableState.errorEmitted)) { process.nextTick(emitErrorNT, this, err); } - return; + return this; } // we set destroyed to true before firing error callbacks in order @@ -39,6 +39,8 @@ function destroy(err, cb) { cb(err); } }); + + return this; } function undestroy() { diff --git a/test/parallel/test-net-socket-destroy-send.js b/test/parallel/test-net-socket-destroy-send.js index 309e41f6c94e6d..6dc4b9566a3900 100644 --- a/test/parallel/test-net-socket-destroy-send.js +++ b/test/parallel/test-net-socket-destroy-send.js @@ -10,7 +10,8 @@ server.listen(0, common.mustCall(function() { const conn = net.createConnection(port); conn.on('connect', common.mustCall(function() { - conn.destroy(); + // Test destroy returns this, even on multiple calls when it short-circuits. + assert.strictEqual(conn, conn.destroy().destroy()); conn.on('error', common.mustCall(function(err) { assert.strictEqual(err.message, 'This socket is closed'); }));