0

I am running a simple program for long polling to check if any change happens in database to send it to browser in Node.js. I know how to use the callback function but I need to return a value from program.query function to be returned because I will use it in the recursive function. Thanks for your help.

var server=http.createServer(function(req,res){
    program.run(res, 5);
});

server.on('listening',function(){
    console.log('ok, server is running');
});

server.listen(9000);


var program = {

    run: function(res, id, old){

        if(old == undefined){
            // I need a value from database to be returned to old variable
            old = program.query(res, id /*, function(){res.end('there') }*/);
        }

        clearTimeout(tt);
        var tt = setTimeout( function(){

            // I need a value from database to be returned to new_ variable
            var new_ = program.query(res, id /*, function(){res.end('there') }*/);

            if(new_ != old){
                // ... Send Response with the change to browser
            }
            else{ // no change in database, so run again
                old = new_;
                program.run(res, id, old);
            }

        }, 2000);    

    },

    query: function(res, id /*,callback*/){

        connection.query('SELECT table1.column1 FROM table1 WHERE table1.id ="'+id+'"', function(err, rows, fields){


            //callback(res,id,rows[0].column1); // I don't want this solution
            return rows[0].column1; // Not working..

        });


    }

};
3
  • 1
    You can't return a value from an async callback because the return value gets ignored. You have to use a callback unless you want to use promises or some other method to deal with async methods. Commented May 13, 2014 at 0:53
  • @mscdex Thanks for your feedback. Would you please show me a sample code how to deal with the recursive function in callback method? Commented May 13, 2014 at 0:55
  • I need to compare the old value with the new, so I need to get the value of new and the value of old, if they are equal (no change) to run the recursive function again. Your solution might not be able to run a recursive function. Would you please show some code if possible? Thanks. Commented May 13, 2014 at 1:14

1 Answer 1

2

Just call it recursively in the callback. It looks exactly what it sounds like:

var program = {

    run: function(res, id, old){

        if(old == undefined){
            program.query(res, id , function(res,id,result){
                old = result;

                clearTimeout(tt);
                var tt = setTimeout( function(){
                    program.query(res, id , function(res,id,new_){
                        if(new_ != old){
                            // ... Send Response with the change to browser
                        }
                        else{ // no change in database, so run again
                            old = new_;
                            program.run(res, id, old);
                        }
                        // not sure where you want to put the res.end()
                        // since I don't really know your logic.
                    });

                }, 2000);
            });
        }
        else {
            // This code is of course identical to the code above
            // so you can refactor it out into its own function.

            clearTimeout(tt);
            var tt = setTimeout( function(){
                program.query(res, id , function(res,id,new_){
                    if(new_ != old){
                        // ... Send Response with the change to browser
                    }
                    else{ // no change in database, so run again
                        old = new_;
                        program.run(res, id, old);
                    }
                    // not sure where you want to put the res.end()
                    // since I don't really know your logic.
                });

            }, 2000);
        }
    },

    query: function(res, id, callback){

        connection.query('SELECT table1.column1 FROM table1 WHERE table1.id ="'+id+'"', function(err, rows, fields){


            callback(res,id,rows[0].column1);    
        });


    }

};
Sign up to request clarification or add additional context in comments.

2 Comments

Many thanks! I will check it and mark it as correct when it works with me :)
Thanks a lot. Appreciate your effort spent on this code! It works perfect!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.