title: Using JQuery when and deferred returning undefined still
body: <p>I'm new to using <code>$.when</code> and <code>$.Deferred()</code>, and I can't seem to get them to work</p>
  
  <p>What I'm trying to do is run a few functions, and when they've all finished trigger a final function</p>
  
  <p>Here are a couple of options I've tried</p>
  
  <p>Option 1 - Returning <code>d1.getRating is not a function</code> (this is a function further up in the script) following docs from <a href="http://api.jquery.com/jquery.when/" rel="nofollow">JQuery</a> as I understood them</p>
  
  <pre><code>// Set Deferred
  var d1 = $.Deferred();
  // Return movie information
  if (idResp[0].type === "movie") {
    // Output Slug
    traktSlug = 'movies/' + idResp[0].movie.ids.slug;
    // Output
    $.when(d1).done(function (ratingValue) {
        console.log('Rating Is: ' + ratingValue);
        outputIMDb(showCheckIn, traktSlug, ratingValue);
    });
    // Get Rating
    d1.getRating(idResp[0].type, idResp[0].movie.ids.trakt);
  }
  </code></pre>
  
  <p>Option 2 - Returning <code>ratingValue is undefined</code></p>
  
  <pre><code>// Return movie information
  if (idResp[0].type === "movie") {
    // Output Slug
    traktSlug = 'movies/' + idResp[0].movie.ids.slug;
    // Output
    $.when(getRating(idResp[0].type, idResp[0].movie.ids.trakt)).done(function (ratingValue) {
        console.log('Rating Is: ' + ratingValue);
        outputIMDb(showCheckIn, traktSlug, ratingValue);
    });
  }
  </code></pre>
  
  <p>Any suggestions or a nudge in the right direction would be much appreciated</p>
  
  <p>Full source code can be <a href="https://github.com/dpDesignz/trakt-for-imdb/blob/master/js/main.js" rel="nofollow">viewed on GitHub</a></p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I realised after reading the <a href="http://api.jquery.com/jquery.when/" rel="nofollow">JQuery docs</a> again that <code>resolve()</code> is not the name of a generic function, so I modified my code, but I'm still getting <code>ratingValue is undefined</code> back</p>
  
  <p>Updated Option 1 Code</p>
  
  <pre><code>// Set Deferred
  var d1 = $.Deferred();
  // Return movie information
  if (idResp[0].type === "movie") {
    // Output Slug
    var traktSlug = 'movies/' + idResp[0].movie.ids.slug;
    // Output Div
    $.when(d1).done(function(ratingValue) {
        console.log('Rating Is: ' + ratingValue);
        outputIMDb(1, traktSlug, ratingValue);
    });
    // Get Rating
    d1.resolve(getRating(idResp[0].type, idResp[0].movie.ids.trakt));
  }
  </code></pre>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>Sorry, my apologies for not including the <code>getRating</code> function. As follows</p>
  
  <pre><code>// Get Rating
  function getRating(type, id, season = 0, episode =0) {
      var slugType = "";
      switch (type) {
          case "movie":
              slugType = "movies";
              break;
          default:
              slugType = "movies";
              break;
      }
      var request = new XMLHttpRequest();
      request.open('GET', 'https://api.trakt.tv/' + slugType + '/' + id + '/ratings');
      request.setRequestHeader('Content-Type', 'application/json');
      request.setRequestHeader('trakt-api-version', '2');
      request.setRequestHeader('trakt-api-key', APP_KEY);
      request.onreadystatechange = function () {
          if (this.readyState === 4) {
              // Get Response and put in array
              var ratingsResp = JSON.parse(this.responseText);
              // Return Rating
              return Math.round(ratingsResp.rating * 10);
          } else {
              return 0;
          }
        };
        request.send();
  }
  </code></pre>