-1

I am having db.js with db related functions, I want to make call to db.js and wait until it returns the query result. But the result is returned after the execution of the db call. Can anyone please help how to solve this.

Code sample:

var Q = require('q');

db= require("./dbaccess.js");

function waitfor(ms){

     var deferred = Q.defer();

     setTimeout(function() {

        deferred.resolve(db);
     }, 5000);
     return deferred.promise;
}

waitfor(2000).done(function(dbcall) {

console.log('contrived example '+ dbcall.query1()); 

});

dbacess.js:

var sql = require('mssql');

var config = {

user: 'xx',

    password: 'xxx',

    server: 'aaa', 

    database: 'RequestCenter',

    stream: true,  

}

this.query1=function() {

sql.connect(config, function(err) {

    var result;
    var request = new sql.Request();
    request.query("select * from dbo.AcAccount where Name like 'AutomationCli%' ");  
    request.on('row', function(row) {
        console.log(row.Name);
        result = row.Name;
    });

    request.on('error', function(err) {
        console.log("err : "+err); 
    });

    request.on('done', function(returnValue) {
        console.log("done"); 
    });
    return result;
});



sql.on('error', function(err) {

console.log("sql err : "+err);

});

}

Output:

contrived example undefined

in db: AutomationClient

Expected output:

in db: AutomationClient

contrived example AutomationClient
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • What does `db.query1()` return? – Jaromanda X Nov 30 '15 at 08:10
  • Please include `dbaccess.js` code – timothyclifford Nov 30 '15 at 08:46
  • added dbaccess.js code – user2677647 Nov 30 '15 at 09:04
  • 1
    "*the result is returned after the execution of the db call*" - that's how asynchrony works; and you won't be able to change this. And not with promises either. – Bergi Nov 30 '15 at 09:49
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/q/14220321/1048572?how-to-return-the-response-from-an-asynchronous-call) – Bergi Nov 30 '15 at 09:51
  • Why not just write your `dbacess` to return promises from your queries once its connected? Your `waitfor` is returning a promise anyway so your going to be using them in your calling code. Instead of trying to wrap everything is some timeout/promise thing. – ste2425 Nov 30 '15 at 10:13
  • i am completely new to promise, i browsed a made this one but its not working. can you please let me know how this promise should be so that it is synchronized. – user2677647 Nov 30 '15 at 10:30
  • `let me know how this promise should be so that it is synchronized` - promises don't make asynchronous code synchronous - stop thinking synchronous, start thinking asynchronous – Jaromanda X Nov 30 '15 at 14:05

1 Answers1

0

Not sure why your main code passes in 2000 for the ms argument and then does a 5000ms timeout, in fact, why are you doing a timeout at all, if that was some attempt to wait for the db function to complete, then you don't need it

If you must use promises - personally I'd use a simple callback for such simple code, however, I get that you want to learn how to use Promises

Your original code looked like it was attempting to return the value of the LAST row.name

This code returns an array of row.name

Not knowing the type of data you'd be getting, I don't know which is correct

dbacess.js

var Q = require('q');
var sql = require('mssql');

var config = {
    user: 'xx',
    password: 'xxx',
    server: 'aaa',
    database: 'RequestCenter',
    stream: true,

}

this.query1 = function() {
    var deferred = Q.defer();
    sql.connect(config, function(err) {
        var result = []; // return all rows - modify as required
        var request = new sql.Request();
        request.query("select * from dbo.AcAccount where Name like 'AutomationCli%' ");
        request.on('row', function(row) {
            console.log(row.Name);
            result.push(row.Name);
        });

        request.on('error', function(err) {
            console.log("err : " + err);
            deferred.reject(err);
        });

        request.on('done', function(returnValue) {
            deferred.resolve(result);
        });
    });

    sql.on('error', function(err) {
        console.log("sql err : " + err);
        deferred.reject(err);
    });
    return deferred.promise;
}

Code sample:

db = require("./dbaccess.js");

db.query1().then(function(result) {
    console.log('contrived example ' + result);
});
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
  • Thank you very much. I have another scenario where i need to open a dialogbox in a web page and check whether the status is completed or not every 1 min until 30 mins. Can you please give me suggestion of how to loop a promise/defer. – user2677647 Dec 01 '15 at 04:24