@@ -239,23 +239,37 @@ added: v0.11.4
239239-->
240240
241241* ` options ` {Object} Options containing connection details. Check
242- [ ` net.createConnection() ` ] [ ] for the format of the options
243- * ` callback ` {Function} Callback function that receives the created socket
244- * Returns: {stream.Duplex}
242+ [ ` net.createConnection() ` ] [ ] for the format of the options. For custom agents,
243+ this object is passed to the custom ` createConnection ` function.
244+ * ` callback ` {Function} (Optional, primarily for custom agents) A function to be
245+ called by a custom ` createConnection ` implementation when the socket is
246+ created, especially for asynchronous operations.
247+ * ` err ` {Error | null} An error object if socket creation failed.
248+ * ` socket ` {stream.Duplex} The created socket.
249+ * Returns: {stream.Duplex} The created socket. This is returned by the default
250+ implementation or by a custom synchronous ` createConnection ` implementation.
251+ If a custom ` createConnection ` uses the ` callback ` for asynchronous
252+ operation, this return value might not be the primary way to obtain the socket.
245253
246254Produces a socket/stream to be used for HTTP requests.
247255
248- By default, this function is the same as [ ` net.createConnection() ` ] [ ] . However,
249- custom agents may override this method in case greater flexibility is desired.
256+ By default, this function behaves identically to [ ` net.createConnection(options) ` ] [ ] ,
257+ synchronously returning the created socket. The optional ` callback ` parameter in the
258+ signature is ** not** used by this default implementation.
250259
251- A socket/stream can be supplied in one of two ways: by returning the
252- socket/stream from this function, or by passing the socket/stream to ` callback ` .
260+ However, custom agents may override this method to provide greater flexibility,
261+ for example, to create sockets asynchronously. When overriding ` createConnection ` :
253262
254- This method is guaranteed to return an instance of the {net.Socket} class,
255- a subclass of {stream.Duplex}, unless the user specifies a socket
256- type other than {net.Socket}.
263+ 1 . ** Synchronous socket creation** : The overriding method can return the
264+ socket/stream directly.
265+ 2 . ** Asynchronous socket creation** : The overriding method can accept the ` callback `
266+ and pass the created socket/stream to it (e.g., ` callback(null, newSocket) ` ).
267+ If an error occurs during socket creation, it should be passed as the first
268+ argument to the ` callback ` (e.g., ` callback(err) ` ).
257269
258- ` callback ` has a signature of ` (err, stream) ` .
270+ The agent will call the provided ` createConnection ` function with ` options ` and
271+ this internal ` callback ` . The ` callback ` provided by the agent has a signature
272+ of ` (err, stream) ` .
259273
260274### ` agent.keepSocketAlive(socket) `
261275
0 commit comments