I am working on a small project where I want to check the status
of a streaming channel
using an API and display that channels status and information if the status is online
Here's what I'm thinking
const channelNames = ['channel1', 'channel2'];
var channels = [];
function Channel(name, status) {
this.name = name;
this.status = status;
}
//build the channels array of Channels
for(var name in channelNames) {
channels.push(new Channel(channelNames[name], null));
}
channels.forEach(function(channel) {
// call the API to get the status
$.getJSON(url, function(data) {
// in here update the status to 'online' or 'offline' depending on
// the response from the API
});
//Add additional properties to the object if the status is now 'online'
if(channel.status === 'online') {
$.getJSON(differentUrl, function(data) {
// in here add more properties to the object depending on the
// response from the API
});
}
});
How can I make these async calls synchronous so that I can build the Channel
objects in the channels
array based on the responses of API calls? After I build the channels
array I'm going to iterate through them to great new HTML elements to display their properties.