There are different work-arounds for this.
One simple way is similar to what you are doing
this.get = function(params) {
if (hasConnected) {
DoGetOperation(params);
} else {
//here you can check the status of the connect. if it is still in
//progress do nothing. if the connection has failed for some reason
//you can retry it. Otherwise send a response back indicating that the
//operation is in progress.
}
}
Another method might be to use the same async callback mechanism for your get function, which would change your method signature to something like this.
this.deferredOperations = new Array();
this.get = function(params, callback) {
if (hasConnected) {
//using a callback as an optional parameter makes the least
//impact on your current function signature.
//If a callback is present send the data back through it,
//otherwise this function simply returns the value (acts synchronously).
if (callback !== null) {
callback(DoGetOperation(params));
} else {
return DoGetOperation(params);
}
} else {
this.deferredOperations.push([params,callback]);
}
}
//connect changes now
this.connect = function(params) {
//Some logic that connects to db and in callback returns if the connection is successful
connectToSomeDB('ConnectionString', function(connectionResult) {
hasConnected = connectionResult;
if (hasConnected && this.deferredOperations.length > 0) {
for (var i=0; i < this.deferredOperations.length; i++) {
var paramFunction = this.deferredOperations.pop();
var params = paramFunction[0];
var func = paramFunction[1];
DoAsyncGetOperation(params, func); //Need to write a new function that accepts a callback
}
}
})
};
HTH