diff --git a/lib/connection.js b/lib/connection.js index 60bce2c0883..8ccf1fbf0e1 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -9,7 +9,8 @@ var utils = require('./utils'), Collection = require(driver + '/collection'), STATES = require('./connectionstate'), MongooseError = require('./error'), - muri = require('muri'); + muri = require('muri'), + PromiseProvider = require('./promise_provider'); /*! * Protocol prefix regexp. @@ -275,8 +276,18 @@ Connection.prototype.open = function(host, database, port, options, callback) { this.host = host; this.port = port; - this._open(callback); - return this; + var self = this; + var Promise = PromiseProvider.get(); + return new Promise.ES6(function(resolve, reject) { + self._open(!!callback, function(error) { + callback && callback(error); + if (error) { + reject(error); + return; + } + resolve(); + }); + }); }; /** @@ -405,8 +416,18 @@ Connection.prototype.openSet = function(uris, database, options, callback) { } - this._open(callback); - return this; + var self = this; + var Promise = PromiseProvider.get(); + return new Promise.ES6(function(resolve, reject) { + self._open(!!callback, function(error) { + callback && callback(error); + if (error) { + reject(error); + return; + } + resolve(); + }); + }); }; /** @@ -432,7 +453,7 @@ Connection.prototype.error = function(err, callback) { * @api private */ -Connection.prototype._open = function(callback) { +Connection.prototype._open = function(emit, callback) { this.readyState = STATES.connecting; this._closeCalled = false; @@ -449,7 +470,7 @@ Connection.prototype._open = function(callback) { if (self._hasOpened) { if (callback) callback(err); } else { - self.error(err, callback); + self.error(err, emit && callback); } return; } @@ -508,6 +529,27 @@ Connection.prototype.onOpen = function(callback) { */ Connection.prototype.close = function(callback) { + var self = this; + var Promise = PromiseProvider.get(); + return new Promise.ES6(function(resolve, reject) { + self._close(function(error) { + callback && callback(error); + if (error) { + reject(error); + return; + } + resolve(); + }); + }); +}; + +/** + * Handles closing the connection + * + * @param {Function} callback + * @api private + */ +Connection.prototype._close = function(callback) { var self = this; this._closeCalled = true; diff --git a/lib/index.js b/lib/index.js index 9f2916c1a7f..4ec1d50c704 100644 --- a/lib/index.js +++ b/lib/index.js @@ -232,12 +232,11 @@ Mongoose.prototype.connect = function() { var conn = this.connection; if (rgxReplSet.test(arguments[0]) || checkReplicaSetInUri(arguments[0])) { - conn.openSet.apply(conn, arguments); + return conn.openSet.apply(conn, arguments); } else { - conn.open.apply(conn, arguments); + return conn.open.apply(conn, arguments); } - return this; }; /** @@ -249,24 +248,28 @@ Mongoose.prototype.connect = function() { */ Mongoose.prototype.disconnect = function(fn) { - var count = this.connections.length, - error; + var error; this.connections.forEach(function(conn) { conn.close(function(err) { - if (error) return; - + if (error) { + return; + } if (err) { error = err; - if (fn) return fn(err); - throw err; } - - if (fn) - --count || fn(); }); }); - return this; + + var Promise = PromiseProvider.get(); + return new Promise.ES6(function(resolve, reject) { + fn && fn(error); + if (error) { + reject(error); + return; + } + resolve(); + }); }; /** diff --git a/test/index.test.js b/test/index.test.js index 2b2e50ede41..f59af4f7a70 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -128,13 +128,11 @@ describe('mongoose module:', function() { cb(new Error('bam')); }; - var failure = {}; - try { - mong.disconnect(); - } catch (err) { - failure = err; - } - assert.equal('bam', failure.message); + mong.disconnect() + .on('error', function (error) { + assert.equal('bam', error.message); + }); + done(); }); });