I run a small Magento store and would like to extend my Checkout with some functionality. Magento uses KnockoutJS and I want to extend a KnockoutJS component with a simple function that returns either true or false depending on the response of an AJAX call:
isEnabled: function () {
var result = false;
$.getJSON('/mycontroller/checkEnabled', function(response) {
// response from the controller is simply "true" or "false", in this case "true"
result = response
})
return result
}
Unfortunately this always returns false, even if the response of the getJSON
is "true". It's because of the AJAX call being asynchronous I think and the return result
being executed immediately instead of waiting for the AJAX to complete.
I have already googled how to solve this and tried a myriad of different solutions over the past hours, async / await, promises, callbacks (from here), but I just don't understand how to get the response value back into my isEnabled
function after the AJAX call is finished.
This is really making my head hurt, as I only have experience with synchronous programming languages so far.
How do I make the isEnabled
simply return the boolean value from the getJSON
AJAX call?