简体   繁体   中英

angular.forEach Resolving Nested Promises

I have to make sequential AJAX calls after retrieving a collection of data. I am having issues resolving the nested promises.

Basically, I need to extend each object returned in my first collection with a property of ActionItems and set it's value with a promise then resolve each promise in the collection.

Any help would be greatly appreciated.

Factory

$http.get(urlBase + 'Project?$expand=Plant,CreatedBy,ModifiedBy,Plant/Contacts').then(function(success){
        var contents = {};
        contents = success.data.d.results;

        return contents;
    })
    .then(function(contents){ 
        var contentPromises = [];
        angular.forEach(contents, function(content) {
            contentPromises.push(
                $http.get(urlBase + "ActionItems?$filter=ProjectId eq " + content.Id ).then(function(success){
                    content['ActionItems'] = success.data.d.results;                         
                })
            );
        });
        return $q.all(contentPromises).then(function() {
            return contents;
        });
    });

Current Output is undefined

Well turns out this method works, but the key to getting your data back is returning it...

//Forgot the return below...
return $http.get(urlBase + 'Project?$expand=Plant,CreatedBy,ModifiedBy,Plant/Contacts').then(function(success){
    var contents = {};
    contents = success.data.d.results;

    return contents;
})
.then(function(contents){ 
    var contentPromises = [];
    angular.forEach(contents, function(content) {
        contentPromises.push(
            $http.get(urlBase + "ActionItems?$filter=ProjectId eq " + content.Id ).then(function(success){
                content['ActionItems'] = success.data.d.results;                         
            })
        );
    });
    return $q.all(contentPromises).then(function() {
        return contents;
    });
});

Thanks for all who helped.

Your issue lies within the $http.get(...).then() part.

The documentation for .then is telling us that "This method returns a new promise which is resolved or rejected via the return value of the successCallback, errorCallback". So the promise returned by .then is different than the one returned by $http.get . And you are responsible of resolving or rejecting it (by returning)! The promise returned by .then is the one pushed to contentPromises .

Thus you need something like this:

angular.forEach(contents, function(content) {
    contentPromises.push(
        $http.get(urlBase + "ActionItems?$filter=ProjectId eq " + content.Id ).then(function(success){
            content['ActionItems'] = success.data.d.results;
            return success;                         
        })
    );
});

You'd do well to implement the errorCallback too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM