In the code below when i call using ninvoke the "then" part does not get executed. The result is as follows
calling thru a function that uses ninvoke dbConnect --> Success: Connected to db!
calling directly dbConnect --> Success: Connected to db! 2222222 - After Called Directly
Why is that?
"use strict";
var theQ = require("q");
var pg = require('pg');
var dbUri = "postgres://postgres:user123@localhost:5432/postgres"; //postgres uri
var client1 = new pg.Client(dbUri);
var client2 = new pg.Client(dbUri);
function dbConnect(dbClient, tag) {
//var myName = arguments.callee.toString().match(/function ([^\(]+)/)[1];
var deferred = theQ.defer();
dbClient.connect(function(err, result) {
if (err) {
console.error("\n" + tag + "\ndbConnect --> Failure: could not connect to db!!!!");
deferred.reject();
}
else {
console.log("\n" + tag + "\ndbConnect --> Success: Connected to db!");
//results[]
deferred.resolve();
}
});
return deferred.promise;
}
function myTestDB(dbClient, tag) {
return theQ
.ninvoke(dbConnect(dbClient, tag))
.then(function() {console.log("333333 - Called after ninvoke");});
}
//main line
myTestDB(client1, "calling thru a function that uses ninvoke");
dbConnect(client2, "calling directly").then(function() {console.log("2222222 - After Called Directly");});