1

Im trying to use the $q.defer, $q.all and promises in order to wait for a list of http requests

i uses this code to call the $q.all

$q.all(promises).then(function(data) {
      console.log('All promises have resolved', data);

      var retVal = Utils.DoStep2();
      console.log(retVal);
});

this function is never called altough i checked and the $http.get is called for all of the values.

            var deferred = $q.defer(); 
           $http.get(requestUrl).
           then(function (data) {
               var p = {
                   data: data,
                   name: name
               };
               pData.push(p);
               deferred.resolve(p);
               return p;
           })
           .catch(function (status) {
               deferred.reject(status);
           });

           promisesList.push(deferred.promise);

Im printing on DoStep2 the length of pData and also the pData using console.log and what i get is the length of 0 and Looking like 0 objects but when i open it it looks like all of the objects are initalized within the $http.get call for each specific call which made me sure that the $http.get response recieved and it's a valid response.

Also the $all isn't called at all what could be wrong?

Thanks for your assistance

7
  • One possibility is that you're not getting success responses for every request. if you have a 400 response, that counts as a rejection to $http.get. Another is a straight runtime error in the .then() (I can't see pData declared, so .push could fail. Commented Apr 1, 2017 at 20:59
  • 2
    Please provide a minimal reproducible example. Also shouldn't need to use $q.defer() since $http itself returns a promise Commented Apr 1, 2017 at 20:59
  • 1
    Other issues: you push on promisesList but call .all on promises. Personally, I minimize use of $q.defer() -- the result of $http.get is a promise anyway, so you can push that directly. Commented Apr 1, 2017 at 21:00
  • 1
    Avoid the deferred antipattern! Commented Apr 1, 2017 at 21:04
  • What is pdata? Is promises the same as promisesList? Commented Apr 1, 2017 at 21:05

1 Answer 1

1

OK Managed to fix it up I used service function getService() and this function returned the promise then in each call i added the getService() promise returned to the promisesList this list i waited for using $all and it worked thanks alot for your assistance.

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

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.