Skip to content

Commit fa1d453

Browse files
benjamingrrvagg
authored andcommitted
doc: check for errors in 'listen' event
In the docs we typically check for errors and surface them. This is IMO a good idea and good practice. This PR adds a check for errors in three places in the `net` docs where it was missing. PR-URL: #4834 Reviewed-By: Roman Reiss <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Roman Klauke <[email protected]>>
1 parent f462320 commit fa1d453

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

doc/api/net.markdown

+15-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ var server = net.createServer((socket) => {
5151
});
5252

5353
// grab a random port.
54-
server.listen(() => {
54+
server.listen((err) => {
55+
if (err) throw err;
5556
address = server.address();
5657
console.log('opened server on %j', address);
5758
});
@@ -523,7 +524,8 @@ Here is an example of a client of the previously described echo server:
523524

524525
```js
525526
const net = require('net');
526-
const client = net.connect({port: 8124}, () => { //'connect' listener
527+
const client = net.connect({port: 8124}, () => {
528+
// 'connect' listener
527529
console.log('connected to server!');
528530
client.write('world!\r\n');
529531
});
@@ -576,8 +578,8 @@ Here is an example of a client of the previously described echo server:
576578

577579
```js
578580
const net = require('net');
579-
const client = net.connect({port: 8124},
580-
() => { //'connect' listener
581+
const client = net.connect({port: 8124}, () => {
582+
//'connect' listener
581583
console.log('connected to server!');
582584
client.write('world!\r\n');
583585
});
@@ -644,15 +646,18 @@ on port 8124:
644646

645647
```js
646648
const net = require('net');
647-
const server = net.createServer((c) => { //'connection' listener
649+
const server = net.createServer((c) => {
650+
// 'connection' listener
648651
console.log('client connected');
649652
c.on('end', () => {
650653
console.log('client disconnected');
651654
});
652655
c.write('hello\r\n');
653656
c.pipe(c);
654657
});
655-
server.listen(8124, () => { //'listening' listener
658+
server.listen(8124, (err) => {
659+
// 'listening' listener
660+
if (err) throw err;
656661
console.log('server bound');
657662
});
658663
```
@@ -667,7 +672,10 @@ To listen on the socket `/tmp/echo.sock` the third line from the last would
667672
just be changed to
668673

669674
```js
670-
server.listen('/tmp/echo.sock', () => { /* 'listening' listener*/ })
675+
server.listen('/tmp/echo.sock', (err) => {
676+
// 'listening' listener
677+
if (err) throw err;
678+
});
671679
```
672680

673681
Use `nc` to connect to a UNIX domain socket server:

0 commit comments

Comments
 (0)