diff --git a/lib/_http_agent.js b/lib/_http_agent.js index e1cfa6d7fc9d17..fb8c2139314cbd 100644 --- a/lib/_http_agent.js +++ b/lib/_http_agent.js @@ -336,18 +336,17 @@ Agent.prototype.reuseSocket = function reuseSocket(socket, req) { }; Agent.prototype.destroy = function destroy() { - const sets = [this.freeSockets, this.sockets]; - for (var s = 0; s < sets.length; s++) { - var set = sets[s]; - var keys = Object.keys(set); - for (var v = 0; v < keys.length; v++) { - var setName = set[keys[v]]; - for (var n = 0; n < setName.length; n++) { - setName[n].destroy(); - } + destroySockets(this.freeSockets); + destroySockets(this.sockets); +}; + +function destroySockets(sockets) { + for (const set of Object.values(sockets)) { + for (const socket of set) { + socket.destroy(); } } -}; +} function handleSocketCreation(agent, request, informRequest) { return function handleSocketCreation_Inner(err, socket) {