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

Remove Promise around Listen #19

Closed
lukeed opened this issue Jan 30, 2018 · 15 comments
Closed

Remove Promise around Listen #19

lukeed opened this issue Jan 30, 2018 · 15 comments

Comments

@lukeed
Copy link
Owner

lukeed commented Jan 30, 2018

This seemed like a good initially just because of then/catch, but there are a few problems with this:

  1. Calling .listen() does not return the Polka instance directly, so it forces listen to always be called last.

  2. It also prevents one-line initializers for a server.

    const server = polka().listen(3000);
    //=> server is Promise, not Polka instance
  3. The top-level then and catch blocks make it seem like catch is the global catch-all handler.

Instead, .listen() would expose the underlying server.listen method directly, meaning that signature is the same as all other servers.

polka().listen(PORT, err => {
  if (err) throw err; // or whatever you want to do
  console.log(`> Ready on localhost:${PORT}`);
});

This would also remove 5 lines of code 😆

@Youngestdev
Copy link

I think the new development is cool !

@lukeed
Copy link
Owner Author

lukeed commented Jan 30, 2018

Sorry @Youngestdev, which do you mean?

@Youngestdev
Copy link

polka().listen(PORT, err => {
  if (err) throw err; // or whatever you want to do
  console.log(`> Ready on localhost:${PORT}`);
});

That looks cool

@lukeed
Copy link
Owner Author

lukeed commented Jan 30, 2018

Got it, thanks!

@yamalight
Copy link

@lukeed my 2c - I like the promise version more (since I've been using async/await a lot).
I think just clarifying what .listen() returns in docs would be sufficient

@lukeed
Copy link
Owner Author

lukeed commented Feb 3, 2018

Thanks @yamalight~! I'm still slightly in favor of removing the Promise version because I'm seeing a lot of early users trying to server = polka().listen(PORT)... especially for tests. The "drawback" is needing to separate this into two lines, which isn't the end of the world but it slightly annoying since no one's used to it. 🤔

@yamalight
Copy link

@lukeed how about allowing to do server = await polka().listen(PORT)?

@lukeed
Copy link
Owner Author

lukeed commented Feb 3, 2018

That's far more complicated and would usePromise on the main application, affecting performance.

@yamalight
Copy link

fair point 😃

@terkelg
Copy link

terkelg commented Mar 5, 2018

I think we should remove the promise. I actually had some problems with. It took me a while to realise that .listen() not returns an http.Server object.

This is also another step towards better express compatibility like #29 ... and less code! What's not to like.

@r0mflip
Copy link

r0mflip commented Apr 25, 2018

Changing to something like

listen(...args) {
  (this.server = this.server || http.createServer()).on('request', this.handler);
  this.server.listen(...args);
  return this.server;
}

would be closer to honouring options for net.Server

@lukeed
Copy link
Owner Author

lukeed commented Apr 25, 2018

Yup! That's actually exactly the code I was going to write. I just (still) haven't decided if this should happen or not. Pros & cons for each side.

@r0mflip
Copy link

r0mflip commented Apr 25, 2018

Yeah. And I think promisification can be done by

// sketchy, untested code ahead

const {promisify} = require('util');
const app = require('polka')();

app.use((req, res) => {
  res.end(200, 'OK');
});

const start = promisify(app.server.listen);  // assuming server already exists

start()
  .then(...)
  .catch(err => ...);

(😜)

@peappeap
Copy link

peappeap commented May 4, 2018

now i was start node index.js and i get ---- attempt to reconnect has failed--- in browser how can i do sir ?

@mrkishi
Copy link

mrkishi commented Sep 7, 2018

How about exposing a promise as a server property?

const server = polka().listen(PORT)
server.started.then(f).catch(e)

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

Successfully merging a pull request may close this issue.

7 participants