Bring Back between two functions - Javascript

Asked

Viewed 27 times

0

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

This form below 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());

1 answer

0

You cannot because Ajax is asynchronous. A solution is to use an callback when the $.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) });
  • thanks dvd, everything went fine here!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.