Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

Commit d530ee6

Browse files
mikealisaacs
authored andcommitted
Issue #2762. Add callback to close function.
1 parent 9633843 commit d530ee6

4 files changed

Lines changed: 11 additions & 10 deletions

File tree

doc/api/http.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ a listener for the ['listening'](net.html#event_listening_) event.
137137
See also [net.Server.listen()](net.html#server.listen).
138138

139139

140-
### server.close()
140+
### server.close([cb])
141141

142142
Stops the server from accepting new connections.
143143
See [net.Server.close()](net.html#server.close).

doc/api/net.markdown

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,11 @@ Stop accepting connections for the given number of milliseconds (default is
162162
one second). This could be useful for throttling new connections against
163163
DoS attacks or other oversubscription.
164164

165-
#### server.close()
165+
#### server.close([cb])
166166

167167
Stops the server from accepting new connections. This function is
168168
asynchronous, the server is finally closed when the server emits a `'close'`
169-
event.
169+
event. Optionally, you can pass a callback to listen for the `'close'` event.
170170

171171

172172
#### server.address()

lib/net.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -885,12 +885,15 @@ function onconnection(clientHandle) {
885885
}
886886

887887

888-
Server.prototype.close = function() {
888+
Server.prototype.close = function(cb) {
889889
if (!this._handle) {
890890
// Throw error. Follows net_legacy behaviour.
891891
throw new Error('Not running');
892892
}
893893

894+
if (cb) {
895+
this.once('close', cb);
896+
}
894897
this._handle.close();
895898
this._handle = null;
896899
this._emitCloseIfDrained();

test/simple/test-net-server-close.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,14 @@ var assert = require('assert');
2727
var net = require('net');
2828

2929
var server = net.createServer(function(socket) {
30-
server.close();
30+
server.close(function() {
31+
assert.equal(server.connections, 0);
32+
});
3133
process.nextTick(function() {
3234
socket.destroy();
3335
});
3436
});
3537

3638
server.listen(common.PORT, function() {
3739
net.createConnection(common.PORT);
38-
});
39-
40-
server.on('close', function() {
41-
assert.equal(server.connections, 0);
42-
});
40+
});

0 commit comments

Comments
 (0)