There is a function in my code that uses ajax to return JSON data:
function fetchTagData(fileName) {
$.ajax({
type: "GET",
dataType: "json",
url: "/tags/find-tag/"+fileName.tag,
success: function(data){
console.log(data);
return data;
}
});
};
console.log(data)
displays the data I need.
However, when I try to use the function:
var result = fetchTagData(fileName);
useResult(result); // this does not work, result is undefined
console.log(result);
console.log(result)
returns undefined
How can I manage the asynchronous nature of JavaScript to ensure that this code runs in the correct order?