node js : should be use alert to invoke the function?

i dont know what happen with my code..i have a Node.js that queries a MySQL db within the route and displays the result to the user. My problem is how do I run the queries and block until queries are done before redirecting the user to the page they requested? if i add alert before call,function run normally and quick response..but if alert disable the function cant return any value,the function like freeze..

this user code to request value to nodejs

function fred(){ //request function from here to fblue
    alert('fred called'); //if i disable alert,the function not return any value
    get('id', function(datmovMar) {
        var obj = JSON.parse(datmovMar);
        var items = Object.keys(obj);
        var output='';
        items.forEach(function(item) {
            output+=obj[item].something+'<br/>';
          alert(output);
        });         
    });
}

function get(id, callback) {
    $.ajax('http://localhost:8000/' + id + '/', {
        type: 'GET',
        dataType: 'json',
        success: function(data) { if ( callback ) callback(data); },
        error  : function()     { if ( callback ) callback(null); }
    });
}

and this code locate in node js

fblue(function(datmovMar){  //call function from here
        res.write(JSON.stringify(datmovMar)); 
        res.end('\n');
});     

function fblue(callback){ 
    var query = connection.query('SELECT something from table'),
        pinMarker = []; 
    query
    .on('error', function(err) {
        console.log( err );
        updateSockets( err );
    })
    .on('result', function( user ) {
        pinMarker.push( user );
    })
    .on('end',function(){
        if(connectionsArray.length) {
            jsonStringx = JSON.stringify( pinMarker );
            callback(jsonStringx);
        }
    }); 
}

i dont know why if alert disable the function cant run normally?

please help...

thanks

You're calling jQuery's $.ajax method which will create an asynchronous javascript request to your server.

This means that the control flow will continue right after you initiated the call to your server.

So you should not expect from fred() function to block until your request has been served, instead you need to rewrite your browser side javascript code in asynchronous way.

jQuery's $.ajax function by default runs asynchronously. That means the request won't be blocked while it's running, so subsequent lines of code will be immediately executed. With async calls, order of execution is not guaranteed.

You can 'cheat' a little bit here and specify the async option as follows:

$.ajax('http://localhost:8000/' + id + '/', {
    type: 'GET',
    dataType: 'json',
    async: false,
    success: function(data) { if ( callback ) callback(data); },
    error  : function()     { if ( callback ) callback(null); }
});

That will force the $.ajax call to NOT return until it is completed. That's not really the JavaScript way of doing things, however, because you lose the efficiencies gained by asynchronous execution. As another KARASZI pointed out, you should write this asynchonously.