title: Creating an effective loop to gather data from an API for each item
body: <p>What I'm trying to achieve is an effective way of passing each item found in a specific attribute through a JSON request in jQuery, so that I can gather information about it from an API. The API is Twitch and each data attribute is a streamers name.</p>
  
  <p>Each streamer would be defined like so -</p>
  
  <p><code>&lt;div streamer='streamer_name'&gt;&lt;/div&gt;</code></p>
  
  <p>and from the API I'd like to gather their status and information. However, the Twitch API does not showcase information about offline streamers in the original API url, and so I have to use two API URL's to get all the required data.</p>
  
  <p>I have created something that works, and as I'm rather new to this, I've most likely missed out on important features required to make this as effective as possible.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$('[streamer]').each(function() {
    var data = $(this);
    var streamer = $(data).attr('streamer');
    $.getJSON('https://api.twitch.tv/kraken/streams/' + streamer, function(x) {
      if (x.stream != null) {
        $(data).append(
          //online data
        );
      } else {
        $.getJSON('https://api.twitch.tv/kraken/channels/' + streamer, function(x) {
          $(data).append(
            //offline data
          );
        });
      }
    })
  })</code></pre>
  </div>
  </div>
  </p>
  
  <p><a href="http://jsfiddle.net/W4Km8/8801/" rel="nofollow">http://jsfiddle.net/W4Km8/8801/</a> for an example of what this code actually does.</p>
  
  <p><strong>EDIT:</strong> There is also a way to search for multiple streamers at once, although again, it does not show any data for those that are offline. </p>
  
  <p><a href="https://api.twitch.tv/kraken/streams?channel=riotgames,scrags" rel="nofollow">https://api.twitch.tv/kraken/streams?channel=riotgames,scrags</a></p>
  
  <p><strong>EDIT2:</strong> To be more specific about my doubts, occasionally it will append the data incorrectly and will break the page layout, or not show up at all. This happens quite frequently upon page refreshes.</p>
  
  <p>Thanks</p>