Skip to content

Commit b1b2801

Browse files
authored
Add onFailure to query#then (brianc#1082)
The promise adapter I had implemented wasn't spec compliant: it didn't accept both `onSuccess` and `onFailure` in the call to `query#then`. This subtly broke yield & async/await because they both rely on `onError` being passed into `Promise#then`. The pool was also not returning the promise after a client was acquired, which broke awaiting on `pool.connect` - this is also fixed now.
1 parent 1dc1dbc commit b1b2801

File tree

6 files changed

+39
-5
lines changed

6 files changed

+39
-5
lines changed

lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ PG.prototype.connect = function(config, callback) {
6868

6969
this._pools[poolName] = this._pools[poolName] || new this.Pool(config);
7070
var pool = this._pools[poolName];
71-
pool.connect(callback);
7271
if(!pool.listeners('error').length) {
7372
//propagate errors up to pg object
7473
pool.on('error', function(e) {
7574
this.emit('error', e, e.client);
7675
}.bind(this));
7776
}
77+
return pool.connect(callback);
7878
};
7979

8080
// cancel the query running on the given client

lib/native/query.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ var NativeQuery = module.exports = function(native) {
3434

3535
util.inherits(NativeQuery, EventEmitter);
3636

37-
NativeQuery.prototype.then = function(callback) {
38-
return this.promise().then(callback);
37+
NativeQuery.prototype.then = function(onSuccess, onFailure) {
38+
return this.promise().then(onSuccess, onFailure);
3939
};
4040

4141
NativeQuery.prototype.catch = function(callback) {

lib/query.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ var Query = function(config, values, callback) {
4040

4141
util.inherits(Query, EventEmitter);
4242

43-
Query.prototype.then = function(callback) {
44-
return this.promise().then(callback);
43+
Query.prototype.then = function(onSuccess, onFailure) {
44+
return this.promise().then(onSuccess, onFailure);
4545
};
4646

4747
Query.prototype.catch = function(callback) {

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
},
2929
"devDependencies": {
3030
"async": "0.9.0",
31+
"co": "4.6.0",
3132
"jshint": "2.5.2",
3233
"lodash": "4.13.1",
3334
"pg-copy-streams": "0.3.0",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
var helper = require('./test-helper')
2+
var co = require('co')
3+
4+
var tid = setTimeout(function() {
5+
throw new Error('Tests did not complete in tme')
6+
}, 1000)
7+
8+
co(function * () {
9+
var client = yield helper.pg.connect()
10+
var res = yield client.query('SELECT $1::text as name', ['foo'])
11+
assert.equal(res.rows[0].name, 'foo')
12+
13+
var threw = false
14+
try {
15+
yield client.query('SELECT LKDSJDSLKFJ')
16+
} catch(e) {
17+
threw = true
18+
}
19+
assert(threw)
20+
client.release()
21+
helper.pg.end()
22+
clearTimeout(tid)
23+
})
24+
.catch(function(e) {
25+
setImmediate(function() {
26+
throw e
27+
})
28+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var semver = require('semver')
2+
if (semver.lt(process.version, '1.0.0')) {
3+
return console.log('yield is not supported in node <= v0.12')
4+
}
5+
require('./yield-support-body')

0 commit comments

Comments
 (0)