JSFiddle - React, Tailwind, and code Playground
HTML
<div id="result"></div>
JavaScript
var favorites = [];
function processFavorites () {
$("#result").empty();
$.each(favorites,function(index,obj){
// here we populate the results div
$('#result').append(obj.title + ' ' + index + ': ' + obj.login + ' (' + obj.viewers + ') Viewers<br />');
});
}
function getFavorites () {
favorites = [];
$.getJSON('http://api.justin.tv/api/user/favorites/hubschrauber.json?jsonp=?', function(favs) {
var defArr = [];
$.each(favs, function(key, value) {
//For every Object, check if user is referenced in stream and therefore online
defArr.push($.getJSON('http://api.justin.tv/api/stream/list.json?channel=' + favs[key].login + '&jsonp=?', function(streams) {
favorites.push({title:value.title,key:key,login:value.login,viewers:streams[0].channel_count});
}));
});
$.when.apply($,defArr).always(function(){
processFavorites();
});
});
}
getFavorites();