{"id":19069722,"url":"https://github.com/evanlucas/do-listen","last_synced_at":"2026-05-18T09:04:27.936Z","repository":{"id":57231512,"uuid":"101935883","full_name":"evanlucas/do-listen","owner":"evanlucas","description":"doListen(server, (err) =\u003e { // err actually exists now })","archived":false,"fork":false,"pushed_at":"2017-09-06T19:21:56.000Z","size":82,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-30T15:53:19.744Z","etag":null,"topics":["node-http","node-net","nodejs"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/evanlucas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-08-30T22:50:24.000Z","updated_at":"2017-08-30T23:19:30.000Z","dependencies_parsed_at":"2022-09-04T15:04:46.349Z","dependency_job_id":null,"html_url":"https://github.com/evanlucas/do-listen","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"purl":"pkg:github/evanlucas/do-listen","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evanlucas%2Fdo-listen","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evanlucas%2Fdo-listen/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evanlucas%2Fdo-listen/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evanlucas%2Fdo-listen/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/evanlucas","download_url":"https://codeload.github.com/evanlucas/do-listen/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evanlucas%2Fdo-listen/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":267205994,"owners_count":24052762,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-26T02:00:08.937Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["node-http","node-net","nodejs"],"created_at":"2024-11-09T01:15:26.766Z","updated_at":"2026-05-18T09:04:22.893Z","avatar_url":"https://github.com/evanlucas.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# do-listen\n\n[![Build Status](https://travis-ci.org/evanlucas/do-listen.svg)](https://travis-ci.org/evanlucas/do-listen)\n[![Coverage Status](https://coveralls.io/repos/evanlucas/do-listen/badge.svg?branch=master\u0026service=github)](https://coveralls.io/github/evanlucas/do-listen?branch=master)\n\nProperly call callback on a node server on the listening or error event.\n\nWhy?\n\nI continue to see the following:\n\n```js\nconst http = require('http')\nconst server = http.createServer()\nserver.listen((err) =\u003e { // `err` won't be passed here\n  if (err) throw err\n})\n```\n\nThe problem with that is that the `cb` passed to the `listen` function is\nonly added as an event listener for the `listening` event.\n\ni.e. no `err` will ever be passed back.\n\nIn order to properly wait for either the listening event or error event\nwithout leaking listeners:\n\n```js\nfunction doListen(server, cb) {\n  var called = false\n  const done = (err) =\u003e {\n    if (called) return\n    server.removeListener('error', done)\n    server.removeListener('listening', done)\n    called = true\n    cb(err)\n  }\n\n  server.on('error', done)\n  server.on('listening', done)\n}\n```\n\nwhich is literally what the package is :]\n\n## Install\n\n```bash\n$ npm install do-listen\n```\n\n## Examples\n\n```js\n'use strict'\n\nconst net = require('net')\nconst doListen = require('do-listen')\n\nconst server = net.createServer()\nserver.listen(0)\ndoListen(server, (err) =\u003e {\n  if (err) throw err\n  console.log('listening on', server.address().port)\n})\n```\n\nThis also works with http\n\n```js\n'use strict'\n\nconst http = require('http')\nconst doListen = require('do-listen')\n\nconst server = http.createServer()\nserver.listen(8080)\ndoListen(server, (err) =\u003e {\n  if (err) throw err\n  console.log('listening on', server.address().port)\n})\n```\n\nWant to use with promises or async/await?\n\n```js\n'use strict'\n\nconst http = require('http')\nconst {promisify} = require('util')\nconst doListen = promisify(require('do-listen'))\n\nconst server = http.createServer()\nserver.listen(8080)\nasync function main() {\n  await doListen(server)\n}\n\nmain()\n// or\n\n'use strict'\n\nconst http = require('http')\nconst {promisify} = require('util')\nconst doListen = promisify(require('do-listen'))\n\nconst server = http.createServer()\nserver.listen(8080)\ndoListen(server).then(() =\u003e {\n  console.log('listening on', server.address().port)\n}).catch((err) =\u003e {\n  throw err\n})\n```\n\n## Test\n\n```bash\n$ npm test\n```\n\n## Author\n\nEvan Lucas\n\n## License\n\nMIT (See `LICENSE` for more info)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevanlucas%2Fdo-listen","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fevanlucas%2Fdo-listen","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevanlucas%2Fdo-listen/lists"}