title: How to return a value from an ajax function containing a each function to the main program?
body: <p>My <code>foo()</code> function returns a <code>1</code> value if a person has logged on the selected machine. It returns <code>0</code> if the machine is available. I need to use this variable (<code>log</code>) in a button click event. However I simply can't figure out how to write a callback in this scenario. </p>
  
  <pre class="lang-js prettyprint-override"><code>function foo(callback) {
    $.ajax({
      type: 'GET',
      url: '/api/machine/',
      dataType: "JSON",
      data: "data",
      success: function(data) {
        callback(data)
  
        $.each(data, function(i, data) {
          if (data.machinename == $("#DropDownList2").val()) {
            var log = data.selected;
            alert('Machine status is: ' + log);
          };
        });
      }
    });
  }
  
  foo(function(data) {
    alert('Data2 is: ' + data);
  });
  </code></pre>