connections. This function is asynchronous, the server is finally
closed when all connections are ended and the server emits a `'close'`
event. Optionally, you can pass a callback to listen for the `'close'`
-event.
+event. If present, the callback is invoked with any potential error
+as the first and only argument.
### server.address()
self._emitCloseIfDrained();
}
- if (!this._handle) {
- // Throw error. Follows net_legacy behaviour.
- throw new Error('Not running');
+ if (cb) {
+ if (!this._handle) {
+ this.once('close', function() {
+ cb(new Error('Not running'));
+ });
+ } else {
+ this.once('close', cb);
+ }
}
- if (cb) {
- this.once('close', cb);
+ if (this._handle) {
+ this._handle.close();
+ this._handle = null;
}
- this._handle.close();
- this._handle = null;
if (this._usingSlaves) {
var self = this,
res.on('end', function() {
assert.equal(res.body, 'hello world\n');
body_ok = true;
- server.close();
+ server.close(function(error) {
+ assert.equal(error, undefined);
+ server.close(function(error) {
+ assert.equal(error && error.message, 'Not running');
+ });
+ });
});
});
assert.ok(status_ok);
assert.ok(headers_ok);
assert.ok(body_ok);
-
- // Double close should throw. Follows net_legacy behaviour.
- assert.throws(function() {
- server.close();
- });
});