mirror of
https://github.com/brianc/node-postgres.git
synced 2025-12-08 20:16:25 +00:00
Add tests to support deprecated event listeners
This commit is contained in:
parent
fc3634045b
commit
5f5e40f03c
@ -374,10 +374,8 @@ Client.prototype.query = function (config, values, callback) {
|
||||
}
|
||||
} else {
|
||||
query = new Query(config, values, callback)
|
||||
result = query.callback ? undefined : new global.Promise((resolve, reject) => {
|
||||
query.once('end', resolve)
|
||||
query.once('error', reject)
|
||||
})
|
||||
query._deprecateListeners()
|
||||
result = query
|
||||
}
|
||||
|
||||
if (this.binary && !query.binary) {
|
||||
|
||||
19
lib/query.js
19
lib/query.js
@ -53,6 +53,25 @@ Query.prototype.requiresPreparation = function() {
|
||||
return this.values.length > 0;
|
||||
};
|
||||
|
||||
Query.prototype.then = function(onSuccess, onFailure) {
|
||||
return this._getPromise().then(onSuccess, onFailure);
|
||||
};
|
||||
|
||||
Query.prototype.catch = function(callback) {
|
||||
return this._getPromise().catch(callback);
|
||||
};
|
||||
|
||||
Query.prototype._getPromise = function() {
|
||||
if (this._promise) return this._promise;
|
||||
this._promise = new Promise(function(resolve, reject) {
|
||||
this.once('end', resolve);
|
||||
this.once('error', reject);
|
||||
}.bind(this));
|
||||
return this._promise;
|
||||
};
|
||||
|
||||
Query.prototype._deprecateListeners = function() {
|
||||
}
|
||||
|
||||
//associates row metadata from the supplied
|
||||
//message with this query object
|
||||
|
||||
30
test/integration/client/deprecated-listener-tests.js
Normal file
30
test/integration/client/deprecated-listener-tests.js
Normal file
@ -0,0 +1,30 @@
|
||||
const helper = require('./test-helper')
|
||||
const pg = helper.pg
|
||||
const suite = new helper.Suite()
|
||||
|
||||
suite.test('Query with a callback should still support event-listeners', (done) => {
|
||||
const client = new pg.Client()
|
||||
const sink = new helper.Sink(3, 1000, () => {
|
||||
client.end()
|
||||
done()
|
||||
})
|
||||
client.connect()
|
||||
const query = client.query('SELECT NOW()', (err, res) => {
|
||||
sink.add()
|
||||
})
|
||||
query.on('row', () => sink.add())
|
||||
query.on('end', () => sink.add())
|
||||
})
|
||||
|
||||
suite.test('Query with a promise should still support event-listeners', (done) => {
|
||||
const client = new pg.Client()
|
||||
const sink = new helper.Sink(3, 1000, () => {
|
||||
client.end()
|
||||
done()
|
||||
})
|
||||
client.connect()
|
||||
const query = client.query('SELECT NOW()')
|
||||
query.on('row', () => sink.add())
|
||||
query.on('end', () => sink.add())
|
||||
query.then(() => sink.add())
|
||||
})
|
||||
Loading…
x
Reference in New Issue
Block a user