I am unable to pass a return state from one function to another. In the first part of code I am calling the printing function in .ajax success method:
$("#print_button").click(function() {
var elem = $(this);
$.ajax({
url: 'print.php',
type: 'POST',
data: "print_id=" + elem.attr('data-print'),
dataType: 'json',
method: 'post',
success: function(data) {
if (data !== null) {
//returnState
if (printnow(data) == true) {
console.log("print success");
} else {
console.log("print failed");
}
} else {
alert("no data to print");
}
}
})
});
The print function in short:
function printnow(beer) {
var returnState = false;
$.get("./servis.label", function(labelXml) {
try {...
var success = label.print(printerName);
if (success == true) {
returnState = true;
} else {
returnState = false;
}
} catch (e) {
alert(e.message || e);
returnState = false;
}
}, "text");
return returnState;
};
If I hit print button I immediately getting the “print failed” message - I think this means that if (printnow(data) == true) is not waiting for function printnow(data) to be completed.
How I can return true if printing was success?
if (success == true) {
returnState = true;