title: How to use a variable returned by $.getJSON in another function
body: <p>I have a javascript object with several properties and methods. I want to call the first method within the second, in order to get the default number of ingredients of a pizza and compare it with another value. However, I detect that no-value is present in the comparison of the second method. </p>
  
  <p>Googling about this issue, I saw that I have to make a callback in the first method, but it didn't work for me. So, how can I be sure that the property <code>obj.app.defaultIngredients</code> will have a value returned by the JSON, when a 'click' event in the second method will occur? And, in that moment, I can compare the value as you also can see in the second method?</p>
  
  <p>There is my (not working) code:</p>
  
  <pre><code>obj = {};
  obj.app = {
  
      defaultIngredients: '',
  
      getDefaultIngredientsNumber: function() {
          $.getJSON('/sites/all/json/pizza.json', function(data) {
              var node = $('.node').attr('data-nid'),
                  node = 'node-' + node; // returns something like 'node-3'
                  $.each(data, function(key, val) {
                    // This returns an integer
               obj.app.defaultIngredients = parseInt(data[node].general.default_ingredients);
                  });
          }).done(function() {
              return obj.app.defaultIngredients;  
          });
      },
  
      customAddToCart: function() {
          $('#button').click(function(){
              var defaultIngredients = obj.app.getDefaultIngredientsNumber();
              var selectedIngredients = 0;    
              if defaultIngredients &gt;= selectedIngredients) {
                  alert('Add some ingredients');
              }
          }
      }   
  };
  </code></pre>
  
  <p>Some help with this will be very apreciated.</p>