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

Consider having Application.listen() return a promise that resolves when the server has started #709

Closed
jrop opened this issue Apr 15, 2016 · 13 comments

Comments

@jrop
Copy link

jrop commented Apr 15, 2016

Currently, the signature of node's Server.listen is:

Server.listen(port, callback) // plus a few more overloads...

The given callback is attached to the Server 'listen' event, that gets emitted once the socket is listening for incoming messages. What would the implications be of Koa's Application.listen returning a promise, in the spirit of all the async/await changes coming down for v2.x?

@PlasmaPower
Copy link
Contributor

That would break passing app to supertest, or anything else that calls .listen. I'd be fine with returning a promise, but I'd keep the callback as long as node does.

@PlasmaPower
Copy link
Contributor

Also, we are currently returning the server from .listen, so returning a promise would be a breaking change either way.

@jrop
Copy link
Author

jrop commented Apr 15, 2016

@PlasmaPower It can still take a callback as an argument, and return a promise at the same time; consider:

  listen() {
    debug('listen');
    const server = http.createServer(this.callback());
    const args = Array.prototype.slice.call(arguments)

    return new Promise(function (resolve, reject) {
      const callback = args[args.length - 1]
      function onListening(err) {
        if (typeof callback == 'function')
          callback(err)
        return err ? reject(err) : resolve(server)
      }

      if (typeof callback == 'function')
        args[args.length - 1] = onListening
      else
        args.push(onListening)
      server.listen.apply(server, args)
    })
  }

@jrop
Copy link
Author

jrop commented Apr 15, 2016

As for returning the server, my code above resolves to the server object, but yes, it is still a breaking change.

@PlasmaPower
Copy link
Contributor

@jrop Yes I know I was suggesting doing exactly what you said. I'll make a PR for it in a bit.

@jrop
Copy link
Author

jrop commented Apr 15, 2016

Awesome, I thought you might be; if you want I can create the PR, otherwise I'm fine with it either way.

@PlasmaPower
Copy link
Contributor

PlasmaPower commented Apr 15, 2016

It looks like this would break pretty much all our tests. I'd prefer to wait until node changes their .listen to change ours. Edit: or maybe we could do something about this when/if we switch the request library.

@fl0w
Copy link
Contributor

fl0w commented Apr 18, 2016

@PlasmaPower you already made notice of this; but this change would be ultra-major IMO. It somewhat ties into #703 as well. This might be something to consider once there's a consensus regarding tests and a potential rewrite?

@jonathanong
Copy link
Member

this would require wrapping the http server, which isn't really worth it. if https://github.com/normalize/mz wrapped it, then maybe, but it shouldn't be done in koa.

@PlasmaPower
Copy link
Contributor

@jonathanong It wouldn't really require to much extra code, if we use event-to-promise we could simply do return eventToPromise(server, 'listened').

@fl0w
Copy link
Contributor

fl0w commented Apr 25, 2016

Maybe this one can be closed as wontfix?

@jonathanong
Copy link
Member

@fl0w agreed!

@Ajaxy
Copy link

Ajaxy commented May 14, 2018

@PlasmaPower eventToPromise(server, 'listened') did not work form me. It seems that that event is not documented anywhere.

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

No branches or pull requests

5 participants