title: How do I return a response from an AJAX call in a KnockoutJS component function?
body: <p>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:</p>
  <pre><code>isEnabled: function () {
      var result = false;
      $.getJSON('/mycontroller/checkEnabled', function(response) {
          // response from the controller is simply &quot;true&quot; or &quot;false&quot;, in this case &quot;true&quot;
          result = response
      })
      return result
  }
  </code></pre>
  <p>Unfortunately this always returns false, even if the response of the <code>getJSON</code> is &quot;true&quot;. It's because of the AJAX call being asynchronous I think and the <code>return result</code> being executed immediately instead of waiting for the AJAX to complete.</p>
  <p>I have already googled how to solve this and tried a myriad of different solutions over the past hours, async / await, promises, callbacks (from <a href="https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call">here</a>), but I just don't understand how to get the response value back into my <code>isEnabled</code> function after the AJAX call is finished.</p>
  <p>This is really making my head hurt, as I only have experience with synchronous programming languages so far.</p>
  <p>How do I make the <code>isEnabled</code> simply return the boolean value from the <code>getJSON</code> AJAX call?</p>