title: Waiting for API call to finish in Javascript before continuing
body: <p>something I've struggled with in the past and am struggling with today is preventing an API/AJAX from continuing until you've recieved your response. currently I'm working with the Facebook API. I need to get a response from a call then return it but what's happening is that my function is returning before I ever get a response from the API call. I know why it's happening, I just can't figure out how to prevent it! Here's my code...</p>
  
  <pre><code>function makeCall(){
  
  var finalresponse = "";
      var body = 'Test post';
  
        FB.api('/me/feed', 'post', { message: body }, function(response) {
          if (!response || response.error) {
           finalresponse = response.error;
          } else {
            finalresponse = 'Post ID: ' + response.id;
          }
        });
        return finalresponse;
  
  }
  </code></pre>
  
  <p>// ----- EDIT</p>
  
  <p>I noticed some people suggested something like this...</p>
  
  <pre><code>function makeCall(){
  var finalresponse = "";
  FB.api('/me/feed', 'post', { message: body }, function(response) {
          if (!response || response.error) {
           finalresponse = response.error;
           return finalresponse;
          } else {
            finalresponse = 'Post ID: ' + response.id;
            return finalresponse;
          }
        });
  }
  </code></pre>
  
  <p>But this returns undefined</p>
  
  <p>// EDIT BASED ON UPDATES</p>
  
  <pre><code>function share_share(action_id){
  
    var finalresponse = makeCall(action_id, process);
    return finalresponse;
  
  }
  
  function makeCall(action_id, callback){
  
  var body = 'Test post';
        FB.api('/me/feed', 'post', { message: body }, function (response) {
          if (!response || response.error) {
           var finalresponse = response.error;
          } else {
            finalresponse = 'Post ID: ' + response.id;
          }
          callback(action_id, finalresponse);
        });
  
  }
  function process(action_id, finalresponse){
    console.log(finalresponse);
  }
  </code></pre>