diff --git a/lib/datasource.js b/lib/datasource.js index b4e31ca9..77bdfe9f 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -137,28 +137,26 @@ DataSource.prototype.setup = function(name, settings) { schema.connect = function(cb) { var schema = this; - if(schema.connected) { - if (cb) { - process.nextTick(cb); - } + if(schema.connected || schema.connecting) { + process.nextTick(function() { + cb && cb(); + }); return; } schema.connecting = true; if (schema.adapter.connect) { - schema.adapter.connect(function(err) { + schema.adapter.connect(function(err, result) { if (!err) { schema.connected = true; schema.connecting = false; schema.emit('connected'); } - if (cb) { - cb(err); - } + cb && cb(err, result); }); } else { - if (cb) { - process.nextTick(cb); - } + process.nextTick(function() { + cb && cb(); + }); } }; }