How can I call the second sequential promise design pattern method in a loop in angularjs? [duplicate]

时间秒杀一切 提交于 2019-12-13 22:22:45

问题


New to angularjs and trying out the promise pattern for the first time -

I have a service utility inside which I have this method -

this.getData= function(url){
        var defer = $q.defer();

        $http({method: 'GET', url: url}).
            success(function(data, status){
                defer.resolve(data);
            })
            .error(function(data, status) {
                defer.reject(status);
            });

        return defer.promise;
    };

Now inside my controller, I am calling a method called A()

   var A = function () {
    $scope.myobjectArray = [];


    return utility.getData("some url").then(funciton(data)
    {

        for (i = 0; i < data.length; i++) {
            $scope.myobjectArray.push(data[i].attribute1, new Array());

        }

    }
    ).
    then(function () {

        return getTheSecondAttributeArray();
    }).catch(function (status) {
//display error

    });


};

var getTheSecondAttributeArray = function () {

    for (i = 0; i < $scope.myObjectArray.length; i++) {
        var secondAttributeArray = [];
        var currentType = $scope.myObjectArray[i];
        utility.getData("some url").then(function (response) {
            for (j = 0; j < response.length; j++) {
//some response manipulation
                secondAttributeArray.push(response[j].text);
            }
        currentType.secondAttribute = secondAttributeArray;
        }).catch(function () {//catch error, display message
        })

    }
}

However, it looks like that the last element of the $scope.myobjectArray (n-1th element) is only getting populated. Also, the secondAttributeArray that this last element contains is a concatenated array of all secondAttributes for all objects of the $scope.myobjectArray.

Cannot figure out what can I change here.

EDIT:

When I tried accessing $scope.myObjectArray[j] inside the 'then' function, it said $scope.myObjectArray[j] was undefined. --> And so I created a currentType variable and assigned $scope.myObjectArray[j] to it and that was easily accessible inside the 'then' function. Weird!

Also, I see that only the last object of the $scope.myObjectArray gets values and not the rest. The rest of the objects in the array are empty

Any help is appreciated.

var myObject = function(firstattribute, secondAttribute){

this.firstattribute = firstattribute;
this.secondAttribute = secondAttribute;
}

The explanation here by Beehive (Angularjs $q.all) is something that I am facing. I only get the last loop's data.


回答1:


The issue is all in function closures. When the get data returns, your currentType is the last one because the for j loop ended already. So what you need to do is move the code starting from utility.getData to a separate method passing the parameters of the currentType and the seccondAttributeArray so the closure will contain them as parameters of the function and not change them as the for j loop progresses.

for (i = 0; i < $scope.myObjectArray.length; i++) { 
  var secondAttributeArray = []; 
  var currentType = $scope.myObjectArray[i];
  fillSecondAttributeArray($scope, secondAttributeArray, currentType);
 }


来源:https://stackoverflow.com/questions/36039450/how-can-i-call-the-second-sequential-promise-design-pattern-method-in-a-loop-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!