Im struggling with this js code for some reason. I can't access variables inside the ajax function. I tried solving this by placing the "formatted" variable above the ajax function but I can't access it from inside. How do I solve this?
angular.module('ireg')
.controller("compoundSearchController", function(){
    var vm = this;
    vm.searchText = "Enter your search here...";
    vm.compounds = getJSON();
    function getJSON(){
        var formatted = "initial";  
        console.log(formatted); // equals initial
        $.ajax({url: "http://localhost:8080/ireg/app/php/returnAllCompounds.php", success: function(result){
            formatted = jQuery.parseJSON( result );
            console.log(formatted); //equals the correct object
        }})
        console.log(formatted); // equals initial
        return formatted;
    }
    console.log(vm.compounds); // equals initial
});
 
     
    