From 76c10005670bdaea6bd01e789060a4b4101f0419 Mon Sep 17 00:00:00 2001 From: "Brian M. Carlson" Date: Fri, 16 Jun 2017 16:11:25 -0500 Subject: [PATCH] Add event-emitters back --- lib/client.js | 1 - lib/native/client.js | 7 +------ lib/native/query.js | 17 +++++++++++++++++ lib/query.js | 3 --- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/lib/client.js b/lib/client.js index c866f293..8ca944b1 100644 --- a/lib/client.js +++ b/lib/client.js @@ -374,7 +374,6 @@ Client.prototype.query = function (config, values, callback) { } } else { query = new Query(config, values, callback) - query._deprecateListeners() result = query } diff --git a/lib/native/client.js b/lib/native/client.js index 639484ec..7950bc3d 100644 --- a/lib/native/client.js +++ b/lib/native/client.js @@ -131,14 +131,9 @@ Client.prototype.query = function(config, values, callback) { } var query = new NativeQuery(config, values, callback); - var result = query.callback ? query : new global.Promise(function(resolve, reject) { - query.on('end', resolve); - query.on('error', reject); - }); - this._queryQueue.push(query); this._pulseQueryQueue(); - return result; + return query; }; //disconnect from the backend server diff --git a/lib/native/query.js b/lib/native/query.js index 1523e7c7..a1f7dd42 100644 --- a/lib/native/query.js +++ b/lib/native/query.js @@ -67,6 +67,23 @@ NativeQuery.prototype.handleError = function(err) { self.state = 'error'; }; +NativeQuery.prototype.then = function(onSuccess, onFailure) { + return this._getPromise().then(onSuccess, onFailure); +}; + +NativeQuery.prototype.catch = function(callback) { + return this._getPromise().catch(callback); +}; + +NativeQuery.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; +}; + NativeQuery.prototype.submit = function(client) { this.state = 'running'; var self = this; diff --git a/lib/query.js b/lib/query.js index a98afaae..d3656748 100644 --- a/lib/query.js +++ b/lib/query.js @@ -70,9 +70,6 @@ Query.prototype._getPromise = function() { return this._promise; }; -Query.prototype._deprecateListeners = function() { -} - //associates row metadata from the supplied //message with this query object //metadata used when parsing row results