Title: Promisify server.listen · Issue #21482 · nodejs/node · GitHub
Open Graph Title: Promisify server.listen · Issue #21482 · nodejs/node
X Title: Promisify server.listen · Issue #21482 · nodejs/node
Description: Problem: const server = app.listen(); console.log(`Server listening on: ${server.address().port}`) can fail, as the server is created asynchronously and using the callback is not always practical/flexible Proposal either server.listenPro...
Open Graph Description: Problem: const server = app.listen(); console.log(`Server listening on: ${server.address().port}`) can fail, as the server is created asynchronously and using the callback is not always practical/f...
X Description: Problem: const server = app.listen(); console.log(`Server listening on: ${server.address().port}`) can fail, as the server is created asynchronously and using the callback is not always practical/f...
Opengraph URL: https://github.com/nodejs/node/issues/21482
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Promisify server.listen","articleBody":"### Problem:\r\n```js\r\nconst server = app.listen();\r\nconsole.log(`Server listening on: ${server.address().port}`)\r\n```\r\ncan fail, as the server is created asynchronously and using the callback is not always practical/flexible\r\n\r\n### Proposal\r\neither `server.listenPromise(port)`, or even better, have `server.listen(port)` return a Promise if no callback is provided\r\n\r\n### Example\r\n```js\r\n\r\n// .... \r\n\r\nconst start = async () => {\r\n await config.load();\r\n await DB.connect(dbOptions);\r\n router(app);\r\n app.use((err, req, res, next) => { // error handler\r\n // ...\r\n });\r\n\r\n const server = http.createServer(app);\r\n // proposal:\r\n return server.listen(process.env.PORT || 3000); // if .listen would be a promise resolving to server\r\n // or await server.listen(...); return server; else\r\n\r\n // currently I have to do:\r\n // return new Promise((res, rej) => server.listen(process.env.PORT || 3000, err => err ? rej(err) : res()));\r\n\r\n // or using express's app.listen, which wraps createServer and listen:\r\n // return new Promise((res, rej) => app.listen(process.env.PORT || 3000, function (err) {\r\n // if (err) return rej(err);\r\n // res(this) // this is node's http Server\r\n // }));\r\n\r\n // using util.promisify doesn't look much better:\r\n // return promisify(server.listen).bind(server)(process.env.PORT || 3000);\r\n // or\r\n // return promisify(cb => server.listen(process.env.PORT || 3000, cb))();\r\n};\r\n```\r\n\r\nthen using it somewhere else:\r\n```js\r\nstart()\r\n .then(server => console.log(`Server listening on: ${server.address().port}`))\r\n .catch(err => {\r\n // ...\r\n });\r\n```\r\n\r\nEdit: I'll try to submit this request to express, and have a similar promise method near https://github.com/expressjs/express/blob/master/lib/application.js#L616-L619\r\n\r\nEdit2: the discussion in expressjs/express#3675 has interesting details","author":{"url":"https://github.com/caub","@type":"Person","name":"caub"},"datePublished":"2018-06-23T07:11:43Z","url":"https://github.com/nodejs/node/issues/21482"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:b1f4a55a-0238-83b0-d0e7-b268daf55107 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CB68:6780E:564907:7070DD:6AED28E6 |
| html-safe-nonce | d2b31040577580fbe65c4af7ef1bc3068baa47b812679b35325bf9fc2ca8ce9e |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQjY4OjY3ODBFOjU2NDkwNzo3MDcwREQ6NkFFRDI4RTYiLCJ2aXNpdG9yX2lkIjoiMTAxMDI3MjM0OTU3NjgzMzI1NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 9249f2120dc820e2c09002f621d1745eca9dc8e705ccea36a12614820a358583 |
| hovercard-subject-tag | issue:335076411 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/nodejs/node/21482/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7592c885cf4594fe1d89a8cc82e4b6ca781d9ac6d540313ba157e5d4e4f68d25/nodejs/node/issues/21482 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7592c885cf4594fe1d89a8cc82e4b6ca781d9ac6d540313ba157e5d4e4f68d25/nodejs/node/issues/21482 |
| og:image:alt | Problem: const server = app.listen(); console.log(`Server listening on: ${server.address().port}`) can fail, as the server is created asynchronously and using the callback is not always practical/f... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | caub |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9d95365dda0576efe9b71f45807595e396bd0ca9286fba5367a05048e6ddf8f2 |
| turbo-cache-control | no-preview |
| go-import | github.com/nodejs/node git https://github.com/nodejs/node.git |
| octolytics-dimension-user_id | 9950313 |
| octolytics-dimension-user_login | nodejs |
| octolytics-dimension-repository_id | 27193779 |
| octolytics-dimension-repository_nwo | nodejs/node |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 27193779 |
| octolytics-dimension-repository_network_root_nwo | nodejs/node |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 98b41fa4b5635ceb45a4c54320fced73817ff290 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width