0

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(); 
}
hanabi_noir
  • 197
  • 1
  • 1
  • 15
  • The `CheckChannelStatus` seems to be Promise-based and it might be done after you've logged the status to console. The promise is asynchronous. – E. Sundin Jul 09 '17 at 02:10
  • I was going to post an answer, but the question had been marked as duplicate and closed before I could. The previous comment is correct, among other things(scope, http and q service usage is not correct or follows best practices), an error you have is that the for loop needs to have a closure or you need to use another kind of loop(forEach, map, etc) to always have the correct value in each iteration. – RGonzalez Jul 09 '17 at 03:09
  • @RGonzalez Thanks for providing the tips. I've updated the question, and I'm trying to fix it. – hanabi_noir Jul 09 '17 at 03:46
  • @hanabinoir Well, this is what I was going to answer : http://text-share.com/view/c8ea471e . I hope it helps, that page screwed up the formatting so hopefully you'll be able to follow the code. Cheers – RGonzalez Jul 09 '17 at 17:22

0 Answers0