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


i dont know happen code..i have node.js queries mysql db within route , displays result user. problem how run queries , block until queries done before redirecting user page requested? if add alert before call,function run , quick response..but if alert disable function cant return value,the function freeze..

this user code request value nodejs

function fred(){ //request function here fblue     alert('fred called'); //if disable alert,the function not return 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 code locate in node js

fblue(function(datmovmar){  //call function here         res.write(json.stringify(datmovmar));          res.end('\n'); });       function fblue(callback){      var query = connection.query('select 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 function cant run normally?

please help...

thanks

you're calling jquery's $.ajax method create asynchronous javascript request server.

this means control flow continue right after initiated call server.

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


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -