I have a javascript function in my code which fetch data from a server using a ajax post request.
function fetchData(keyword)
{
$.post(
'http://example.com/apiv5/ajax/',
{
command: 'fetchxxx',
token: 'xxxyyyzzz',
data: keyword
},
function(data)
{
return data;
}
);
}
i need to concatenate the data into a variable like this, here i actually call the function.
var msg = '<p class="question">'+thisGuy.data('description')+'</p>'+fetchData(thisGuy.data('title'));
Note that here thisGuy = $(this);
Now the problem is , the fetchData()
function take few seconds to fetch and load the data but in the meantime javascript skip this process and put a 'undefined' as if the returned value of this function.
Now how can i tell them to wait until it fetch the data and once it done then concatenate data in that variable? i have seen the wait() method but can not understand how to use it.