
Product
Introducing Webhook Events for Alert Changes
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.
Properly call callback on a node server on the listening or error event.
Why?
I continue to see the following:
const http = require('http')
const server = http.createServer()
server.listen((err) => { // `err` won't be passed here
if (err) throw err
})
The problem with that is that the cb passed to the listen function is
only added as an event listener for the listening event.
i.e. no err will ever be passed back.
In order to properly wait for either the listening event or error event without leaking listeners:
function doListen(server, cb) {
var called = false
const done = (err) => {
if (called) return
server.removeListener('error', done)
server.removeListener('listening', done)
called = true
cb(err)
}
server.on('error', done)
server.on('listening', done)
}
which is literally what the package is :]
$ npm install do-listen
'use strict'
const net = require('net')
const doListen = require('do-listen')
const server = net.createServer()
server.listen(0)
doListen(server, (err) => {
if (err) throw err
console.log('listening on', server.address().port)
})
This also works with http
'use strict'
const http = require('http')
const doListen = require('do-listen')
const server = http.createServer()
server.listen(8080)
doListen(server, (err) => {
if (err) throw err
console.log('listening on', server.address().port)
})
Want to use with promises or async/await?
'use strict'
const http = require('http')
const {promisify} = require('util')
const doListen = require('do-listen')
const server = http.createServer()
server.listen(8080)
async function main() {
await doListen(server)
}
// or
'use strict'
const http = require('http')
const {promisify} = require('util')
const doListen = require('do-listen')
const server = http.createServer()
server.listen(8080)
doListen(server).then(() => {
console.log('listening on', server.address().port)
}).catch((err) => {
throw err
})
$ npm test
Evan Lucas
MIT (See LICENSE for more info)
FAQs
Listen to a server and call a callback on error or success
We found that do-listen demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Add real-time Socket webhook events to your workflows to automatically receive software supply chain alert changes in real time.

Security News
ENISA has become a CVE Program Root, giving the EU a central authority for coordinating vulnerability reporting, disclosure, and cross-border response.

Product
Socket now scans OpenVSX extensions, giving teams early detection of risky behaviors, hidden capabilities, and supply chain threats in developer tools.