Skip to content

Commit 1c8b6b9

Browse files
brianccharmander
andauthored
Call callback when end called on unconnected client (#2109)
* Call callback when end called on unconnected client Closes #2108 * Revert a bit of the change * Use readyState because pending doesn't exist in node 8.x * Update packages/pg/lib/client.js use bring your own promise Co-Authored-By: Charmander <[email protected]> Co-authored-by: Charmander <[email protected]>
1 parent 2987753 commit 1c8b6b9

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

packages/pg/lib/client.js

+9
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,15 @@ Client.prototype.query = function (config, values, callback) {
545545
Client.prototype.end = function (cb) {
546546
this._ending = true
547547

548+
// if we have never connected, then end is a noop, callback immediately
549+
if (this.connection.stream.readyState === 'closed') {
550+
if (cb) {
551+
cb()
552+
} else {
553+
return this._Promise.resolve()
554+
}
555+
}
556+
548557
if (this.activeQuery || !this._queryable) {
549558
// if we have an active query we need to force a disconnect
550559
// on the socket - otherwise a hung query could block end forever
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
"use strict"
2+
var helper = require('./../test-helper')
3+
const suite = new helper.Suite()
4+
5+
suite.test('Closing an unconnected client calls callback', (done) => {
6+
const client = new helper.pg.Client()
7+
client.end(done)
8+
})
9+
10+
suite.testAsync('Closing an unconnected client resolves promise', () => {
11+
const client = new helper.pg.Client()
12+
return client.end()
13+
})

0 commit comments

Comments
 (0)