Bring Return Between Two Functions - Javascript

0
Hello, I have the fuction list, which brings a GET url to another function.
How do I get the return from {data.name} out of the function list?

This form in low does not return: /

function list(){

$.get( "<?php echo BASE_URL;?>/open/start/list", function( data ) {

data = typeof data == 'string' ? JSON.parse(data) : data;

return data.name;

});


}
//Trazer o retorno de data.name
console.log(list());
    
asked by anonymous 08.02.2018 / 23:36

1 answer

0

You can not because Ajax is asynchronous. One solution is to use a callback when $.get is processed:

function list(callback){
   $.get( "<?php echo BASE_URL;?>/open/start/list" ).done(function(data){
      data = typeof data == 'string' ? JSON.parse(data) : data;
      callback(data);
   });
}

//Trazer o retorno de data.name
list(function(data){ console.log(data.name) });
    
09.02.2018 / 00:58