Angular Promise - $q.all not returning result and calling Post method several times not working

Zaki

Am trying to call a Post method and then depending on the result I am going to call same Post method multiple times and return the result, using $q.all.

My Post method is :

getData: function (params, callback) {
                $http.post('service/myService', params)
                    .success(function (data) {
                        callback(null, data);
                    }).error(function (error) {
                        callback(error);
                    });
            }

I am calling it in below function, this function is recursive so if it contains nextUrl I am doing same thing until there is no object for paging:

var result = [];
var promises = [];
var checkForPaging = function (nextUrl, service, $q) {
    var deferred = $q.defer();
    var criteria = {
        url: url
    }
    var promise = service.getData(criteria, function (error, data) {
        if (data.statusCode == 200) {
            for (var i = 0; i < data.body.items.length; i++) {
                result.push(data.body.items[i]);
            }
            if (data.body.paging != null && data.body.paging.next != null) {
                checkForPaging(data.body.paging.next.url, service, $q);
            } else{
               deferred.resolve(result);
            }
        }
    });

    promises.push(promise);
    $q.all(promises)
    return deferred.promise;
}

Then am calling this function from below and want to get the result back once all calls are complete:

checkForPaging(data.body.paging.next.url, myService, $q).then(function (data) {
        console.log(data)
    });

The issue I am having is that it never hits the callback function above : console.log(data). But I can see it calling the Post method several times.

If I resolve it like below then I can see after first Post it is hitting the callback above:

$q.all(promises).then(function (results) {
           deferred.resolve(result);
       }, function (errors) {
           deferred.reject(errors);            
       });

Am I doing it right? How can I get the result back and call the Post method several times?

Let me know if it is not clear or have any questions!

Kindzoku

Here is solution: https://plnkr.co/edit/HqkFNo?p=preview I rewrited logic a bit. You can catch the main idea.

UPDATE added promiseless solution

1) Your service should return only promise:

this.getData = function(url, params){
    return $http.post(url, params); //You're just returning promise
}

2) You don't need $q.all, you can use single promise:

var result = [];

var deferred;

var checkForPaging = function (url) {

    if(!deferred){
        deferred = $q.defer();
    }

    //resolvind service promise
    newService.getData(url).then(
        //if ok - 200
        function(response){
            for (var i = 0; i < data.body.items.length; i++) {
                result.push(data.body.items[i]);
            }

            if (data.body.paging != null && data.body.paging.next != null){
            {
                return checkForPaging(data.body.paging.next.url);
            } else{
               deferred.resolve(result);
            }
        },
        //handle errors here
        function(error) {

        }
    );

    return deferred.promise;
}

3) You should call it like this:

checkForPaging('url').then(function(data){
    //here will be resolved promise
    console.log(data);
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

calling a function several times with saving result

From Dev

Calling ajax several times result a duplicated response

From Java

Spring Boot: Calling a @Bean method several times

From Dev

Angular $q returning promise multiple $http calls

From Dev

what the result if unused method called several times?

From Dev

Angular: Returning a $q.defer().promise instead of an $http promise

From Dev

Angular: use $q.all for optional promise?

From Dev

Keep the method somewhere on the view page, instead of calling it several times

From Dev

$q.all() not calling fail handler if one promise fails

From Dev

Calling a method inside *ngIf mutliple times angular

From Dev

Multiple times method calling from angular template

From Dev

.NET how to prevent calling a method returning a string multiple times

From Javascript

axios post promise returning error disallowing accessing result

From Dev

Why calling method for two times result in TypeError: object is not callable

From Javascript

Meteor: Calling an asynchronous function inside a Meteor.method and returning the result

From Dev

Async promise not returning result

From Dev

Promise returning unexpected result

From Dev

angular post method is not working properly

From Dev

Modal popup is not working properly, its calling a function multiple times - click event handler runs several times

From Dev

Returning an AngularJS $q promise with TypeScript

From Dev

Angular RXJS calling http post request is not working

From Dev

Ant calling targets several times

From Dev

Returning an array of promises for Promise.all using filter not working

From Dev

Angular $q.all gets called after first promise finished

From Dev

Finder Method returning only count when calling all()

From Dev

Generic class not returning value all the properties to calling method

From Dev

Returning promise data to the calling function

From Dev

Angular: http GET method is not working just after calling http POST, but its working when i enclose the GET method with setTimeout() method. Why?

From Dev

Angular2 ngfor calling method 3times

Related Related

  1. 1

    calling a function several times with saving result

  2. 2

    Calling ajax several times result a duplicated response

  3. 3

    Spring Boot: Calling a @Bean method several times

  4. 4

    Angular $q returning promise multiple $http calls

  5. 5

    what the result if unused method called several times?

  6. 6

    Angular: Returning a $q.defer().promise instead of an $http promise

  7. 7

    Angular: use $q.all for optional promise?

  8. 8

    Keep the method somewhere on the view page, instead of calling it several times

  9. 9

    $q.all() not calling fail handler if one promise fails

  10. 10

    Calling a method inside *ngIf mutliple times angular

  11. 11

    Multiple times method calling from angular template

  12. 12

    .NET how to prevent calling a method returning a string multiple times

  13. 13

    axios post promise returning error disallowing accessing result

  14. 14

    Why calling method for two times result in TypeError: object is not callable

  15. 15

    Meteor: Calling an asynchronous function inside a Meteor.method and returning the result

  16. 16

    Async promise not returning result

  17. 17

    Promise returning unexpected result

  18. 18

    angular post method is not working properly

  19. 19

    Modal popup is not working properly, its calling a function multiple times - click event handler runs several times

  20. 20

    Returning an AngularJS $q promise with TypeScript

  21. 21

    Angular RXJS calling http post request is not working

  22. 22

    Ant calling targets several times

  23. 23

    Returning an array of promises for Promise.all using filter not working

  24. 24

    Angular $q.all gets called after first promise finished

  25. 25

    Finder Method returning only count when calling all()

  26. 26

    Generic class not returning value all the properties to calling method

  27. 27

    Returning promise data to the calling function

  28. 28

    Angular: http GET method is not working just after calling http POST, but its working when i enclose the GET method with setTimeout() method. Why?

  29. 29

    Angular2 ngfor calling method 3times

HotTag

Archive