Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make app.listen() return a promise #482

Closed
felixfbecker opened this issue Oct 2, 2015 · 5 comments
Closed

Make app.listen() return a promise #482

felixfbecker opened this issue Oct 2, 2015 · 5 comments

Comments

@felixfbecker
Copy link
Contributor

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.

@juliangruber
Copy link
Contributor

you could just write your own lib that does return Promise(http.createServer(app.callback).listen) or something like that. There's no notion of Promise in koa yet and I assume other maintainers will agree that it's better to keep them out for now, at least until a new composing layer with async support lands.

@hallas
Copy link
Contributor

hallas commented Oct 3, 2015

what @juliangruber said

@tejasmanohar
Copy link
Member

👍 for what @juliangruber said

@felixfbecker
Copy link
Contributor Author

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.

@tejasmanohar
Copy link
Member

@felixfbecker works w/ bluebird :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants