Skip to content

Commit d87031d

Browse files
cjihrigcodebytere
authored andcommitted
http: return this from ClientRequest#destroy()
This commit updates ClientRequest#destroy() to return `this` for consistency with other writable streams. PR-URL: #32789 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent c795955 commit d87031d

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

doc/api/http.md

+5
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,11 @@ is finished.
646646
### `request.destroy([error])`
647647
<!-- YAML
648648
added: v0.3.0
649+
changes:
650+
- version: REPLACEME
651+
pr-url: https://github.com/nodejs/node/pull/32789
652+
description: The function returns `this` for consistency with other Readable
653+
streams.
649654
-->
650655

651656
* `error` {Error} Optional, an error to emit with `'error'` event.

lib/_http_client.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ ClientRequest.prototype.abort = function abort() {
349349

350350
ClientRequest.prototype.destroy = function destroy(err) {
351351
if (this.destroyed) {
352-
return;
352+
return this;
353353
}
354354
this.destroyed = true;
355355

@@ -365,6 +365,8 @@ ClientRequest.prototype.destroy = function destroy(err) {
365365
} else if (err) {
366366
this[kError] = err;
367367
}
368+
369+
return this;
368370
};
369371

370372
function _destroy(req, socket, err) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
// Test that http.ClientRequest,prototype.destroy() returns `this`.
4+
require('../common');
5+
6+
const assert = require('assert');
7+
const http = require('http');
8+
const clientRequest = new http.ClientRequest({ createConnection: () => {} });
9+
10+
assert.strictEqual(clientRequest.destroyed, false);
11+
assert.strictEqual(clientRequest.destroy(), clientRequest);
12+
assert.strictEqual(clientRequest.destroyed, true);
13+
assert.strictEqual(clientRequest.destroy(), clientRequest);

0 commit comments

Comments
 (0)