4

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

2
  • And what isn't working with your code? Commented Jan 6, 2015 at 14:58
  • It returns undefined Commented Jan 6, 2015 at 15:00

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Consider using .map on contents.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.