I understand that I used the async function incorrectly. Thanks to provide me the similar question about closure inside loops, but I still need someone to help applying it with my CheckChannelStatus
function.
I would like to change the status
of channelInfo
to offline
according to conditions. It output correct results in one of the functions:
function (response) {
var data = response.data,
channelInfo = {};
scope.channels = [];
console.log(data);
if (data.status && data.status != 200) {
channelInfo = {
name: data.status,
display_name: data.error,
logo: "",
status: data.message,
url: "#"
}
} else {
channelInfo = {
name: data.name,
display_name: data.display_name,
logo: data.logo,
url: data.url
};
CheckChannelStatus(
channelInfo.name, http, q
).then(
function(status) {
channelInfo["status"] = status; //status = "offline"
}
);
}
scope.channels.push(channelInfo); //status = "offline"
}
However, in another function, the status
property keeps remain the same:
function(response) {
var follows = response.data.follows;
scope.channels = [];
for (var i = 0; i < follows.length; i++) {
var channelInfo = follows[i].channel;
if (follows[i].channel.status == null) {
channelInfo.status = "offline";
} else {
CheckChannelStatus(
channelInfo.name, http, q
).then(
function(status) {
channelInfo["status"] = status; //status = "offline"
}
);
}
console.log(channelInfo.status);
scope.channels.push(channelInfo); //status != "offline"
}
console.log(scope.channels);
}
I also tried to remove the status
property from the object within else
, like it is in the first function, but it didn't work.
The functions are inside in an angular controller, the first is loaded at beginning, while the seconded is loaded by ng-click and ng-enter:
function TwitchStream($scope, $http, $q) {
$scope.findChannel = function() {
FindChannel($scope, $http, $q); //ng-click, ng-enter
}
$scope.fccFollows = function() {
FccFollows($scope, $http, $q);
}
$scope.fccFollows();
}