question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make app.listen() return a promise

See original GitHub issue

Node’s native listen() method takes a callback that gets executed once the server is ready. I expected the koa listen() to return a promise, but apparently it doesn’t. This would be very helpful when setting up tests, for example in a mocha before hook, which understands promises:

describe('my app', function () {
  before(async function () {
    let app = koa()
    app.use(router)
    await app.listen()
  })
})

This example of course would also work without async/await by returning the promise.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:13
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

5reactions
felixfbeckercommented, Oct 5, 2015

Yeah I do this now

Promise.promisifyAll(http.Server.prototype)
...
let server = http.createServer(app.callback())
await server.listenAsync()
...
await server.closeAsync()

This works, I’m just sick of promisifying everything. But maybe mz will add http one day. But I must disagree with @juliangruber, there is a notion of promises in koa… Koa supports async functions as middleware, yielding of promises, … co moved to promises and I think koa should slowly move too, since async/await is definitly the future. But this “small” change is really out of scope if I look at the tests and they rely on getting the httpServer from app.listen() everywhere.

0reactions
tejasmanoharcommented, Oct 5, 2015

@felixfbecker works w/ bluebird 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Express app.listen returns Promise { Server {} } - Stack Overflow
I have a startup function that connects to database, then returns a Server with app.listen(). The issue is that if I return app.listen...
Read more >
Promisify server.listen · Issue #21482 · nodejs/node - GitHub
Problem: const server = app.listen(); console.log(`Server listening ... I'd too prefer that listen() would return a Promise whenever there ...
Read more >
Express.js | app.listen() Function - GeeksforGeeks
listen() function is used to bind and listen the connections on the specified host and port. This method is identical to Node's http.Server....
Read more >
Using Promises With Express JS - Turtle-Techies
In this article, we'll look at a variety of tactics developers use in the wild to promisify their Express apps.
Read more >
Using Express.js Routes for Promise-based Error Handling
Find out how to enable promise-based route code and centralize both error ... Here we create an Express.js app and add some basic...
Read more >

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found