http: return this from ClientRequest#destroy()

This commit updates ClientRequest#destroy() to return `this`
for consistency with other writable streams.

PR-URL: https://github.com/nodejs/node/pull/32789
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
cjihrig 2020-05-08 21:25:55 -04:00
parent ff6535a433
commit cad76da198
No known key found for this signature in database
GPG Key ID: 7434390BDBE9B9C5
3 changed files with 21 additions and 1 deletions

View File

@ -631,6 +631,11 @@ is finished.
### `request.destroy([error])`
<!-- YAML
added: v0.3.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/32789
description: The function returns `this` for consistency with other Readable
streams.
-->
* `error` {Error} Optional, an error to emit with `'error'` event.

View File

@ -349,7 +349,7 @@ ClientRequest.prototype.abort = function abort() {
ClientRequest.prototype.destroy = function destroy(err) {
if (this.destroyed) {
return;
return this;
}
this.destroyed = true;
@ -365,6 +365,8 @@ ClientRequest.prototype.destroy = function destroy(err) {
} else if (err) {
this[kError] = err;
}
return this;
};
function _destroy(req, socket, err) {

View File

@ -0,0 +1,13 @@
'use strict';
// Test that http.ClientRequest,prototype.destroy() returns `this`.
require('../common');
const assert = require('assert');
const http = require('http');
const clientRequest = new http.ClientRequest({ createConnection: () => {} });
assert.strictEqual(clientRequest.destroyed, false);
assert.strictEqual(clientRequest.destroy(), clientRequest);
assert.strictEqual(clientRequest.destroyed, true);
assert.strictEqual(clientRequest.destroy(), clientRequest);