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.

Remove Promise around Listen

See original GitHub issue

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 😆

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

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

That looks cool

1reaction
lukeedcommented, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

resolving a promise with EventListener - Stack Overflow
I have tried and although I can resolve the promise, I cannot remove the event handler. What would be a different solution here?...
Read more >
Using promises - JavaScript - MDN Web Docs
A Promise is an object representing the eventual completion or failure of an asynchronous operation. Since most people are consumers of ...
Read more >
Error handling with promises
When a promise rejects, the control jumps to the closest rejection handler down ... executor and promise handlers has an "invisible try..catch "...
Read more >
How to cancel a promise in javascript ? - DEV Community ‍ ‍
Promise cannot be cancelled, it is the process that returns promise must be cancellable. For example, XmlHttpRequest is cancellable as it has an ......
Read more >
How to make a Promise out of a Callback function in JavaScript
In the code above, I put the callback-based request function inside a Promise wrapper Promise( (resolve, reject) => { //callback function}) . ...
Read more >

github_iconTop Related Medium Post

No results found

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