@sovpro/promise-connect
v1.1.2
Published
Promise a connected socket
Downloads
4
Readme
Promise Connect
Promise a connected socket.
Overview
Promise Connect accepts the same parameters as net.createConnection
and net.connect
Usage
try {
const socket = await promiseConnect (port, host)
}
catch (error) {
// handle error
}
Connect Timeout
A non-standard (opt-in) option, connectTimeout
, representing the milliseconds to wait for a connection is available. If not specified the default value for connectTimeout is 0, disabling the connect timeout.
If connectTimeout
is specified, and a connection is not made before the time specified in connectTimeout
has passed, an error with message set to 'Connect timeout' is thrown.
try {
const socket = await promiseConnect ({
port
, host
, connectTimeout: 5000
})
}
catch (error) {
// error.message might be 'Connect timeout'
}
Connect Listener
If a connect listener is specified, it is called after promise fulfillment.
try {
const socket = await promiseConnect (
port
, host
, onConnect
)
function onConnect () {
const ip_addr = socket.address().address
console.log ('Connected to ' + address)
}
}
catch (error) {
// handle error
}