I have a function which makes an ajax post when the server returns a a successful response I wan't to set a flag variable and return it, whatever happens it won't change the flag variable outside the ajax call, if I log it in the success callback it is correct but when I log it again at the very end of the function before I return it it is always the default value.
function addTo(objId, name){
   flag = null;
   $.ajax({
        type: "POST",
        url: "/prcesses/add",
        dataType: 'json',
        data:  {
            name: name,
            id: objId
       },
       cache: false, 
       success: function(response) {     
            flag = response.success;
      }
   });
  return flag;
}
Can anyone tell me how my scope is wrong?
 
    