Skip to content

Commit dc56b9d

Browse files
committed
remove Promise around .listen method;
- Closes #19
1 parent 17c54a7 commit dc56b9d

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

packages/polka/index.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,10 @@ class Polka extends Router {
5959
return this; // chainable
6060
}
6161

62-
listen(port, hostname) {
62+
listen() {
6363
(this.server = this.server || http.createServer()).on('request', this.handler);
64-
return new Promise((res, rej) => {
65-
this.server.listen(port, hostname, err => err ? rej(err) : res());
66-
});
64+
this.server.listen.apply(this.server, arguments);
65+
return this;
6766
}
6867

6968
handler(req, res, info) {

readme.md

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ polka()
5959
console.log(`~> Hello, ${req.hello}`);
6060
res.end(`User: ${req.params.id}`);
6161
})
62-
.listen(3000).then(_ => {
62+
.listen(3000, err => {
63+
if (err) throw err;
6364
console.log(`> Running on localhost:3000`);
6465
});
6566
```
@@ -77,7 +78,7 @@ Type: `Server`<br>
7778

7879
A custom, instantiated server that the Polka instance should attach its [`handler`](#handlerreq-res-parsed) to. This is useful if you have initialized a server elsewhere in your application and want Polka to use _it_ instead of creating a new `http.Server`.
7980

80-
Polka _only_ updates the server when [`polka.listen`](#listenport-hostname) is called. At this time, Polka will create a [`http.Server`](https://nodejs.org/api/http.html#http_class_http_server) if a server was not provided via `options.server`.
81+
Polka _only_ updates the server when [`polka.listen`](#listen) is called. At this time, Polka will create a [`http.Server`](https://nodejs.org/api/http.html#http_class_http_server) if a server was not already provided via `options.server`.
8182

8283
> **Important:** The `server` key will be `undefined` until `polka.listen` is invoked, unless a server was provided.
8384
@@ -134,11 +135,26 @@ app.parse = require('parseurl');
134135
//=> Done!
135136
```
136137

137-
### listen(port, hostname)
138+
### listen()
138139

139-
Returns: `Promise`
140+
Returns: `Polka`
140141

141-
Wraps the native [`server.listen`](https://nodejs.org/dist/latest-v9.x/docs/api/http.html#http_server_listen) with a Promise, rejecting on any error.
142+
Boots (or creates) the underlying [`http.Server`](https://nodejs.org/dist/latest-v9.x/docs/api/http.html#http_class_http_server) for the first time. All arguments are passed to [`server.listen`](https://nodejs.org/dist/latest-v9.x/docs/api/net.html#net_server_listen) directly with no changes.
143+
144+
As of `v0.5.0`, this method no longer returns a Promise. Instead, the current Polka instance is returned directly, allowing for chained operations.
145+
146+
```js
147+
// Could not do this before 0.5.0
148+
const { server, handler } = polka().listen();
149+
150+
// Or this!
151+
const app = polka().listen(PORT, onAppStart);
152+
153+
app.use('users', require('./users'))
154+
.get('/', (req, res) => {
155+
res.end('Pretty cool!');
156+
});
157+
```
142158

143159
### handler(req, res, parsed)
144160

0 commit comments

Comments
 (0)