Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

fix: handle err on both start and stop echo-server #569

Merged
merged 3 commits into from
Dec 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/utils/echo-http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,21 @@ module.exports.createServer = () => {

const server = http.createServer(handler)

server.start = (opts) => new Promise(
(resolve) => server.listen(Object.assign({ port: defaultPort, host: '127.0.0.1' }, opts), resolve)
)
server.start = (opts) => new Promise((resolve, reject) => {
server.once('error', reject)
server.listen(Object.assign({ port: defaultPort, host: '127.0.0.1' }, opts), () => {
server.removeListener('error', reject)
resolve()
})
})

server.stop = () => new Promise((resolve) => server.close(resolve))
server.stop = () => new Promise((resolve, reject) => {
server.once('error', reject)
server.close((err) => {
server.removeListener('error', reject)
err ? reject(err) : resolve()
})
})

return server
}
Expand Down