First, keep in mind that an http request is an asynchronous operation and the results whether a response or a timeout are event driven. So, nothing here blocks until the response or timeout happens. Instead, you register callbacks or listeners for events and you response to those.
More specifically, you can use the timeout
feature for http.get()
. Doc here.
request.setTimeout(timeout[, callback])
timeout <number> Milliseconds before a request times out.
callback <Function> Optional function to be called when a timeout occurs.
Same as binding to the 'timeout' event.
Returns: <http.ClientRequest>
Once a socket is assigned to this request and is connected
`socket.setTimeout()` will be called.
If it sees no response within the timeout time you set, then it will generate the timeout event, call the callback and close down the socket. You can use this as an event driven conclusion to the socket in the same way you might be waiting for the data
event.