Wednesday 15 February 2012

Chaining an unknown number of promises in AngularJS -


OK, suppose I have n an array of XHR requests:

  var promises = []; Var AllResults = []; $ Scope.requests = [["Yuri": "http: // foo / bar / action1", "name": "action1"}, {"yuri": "http: // bar / foo / action2" "Name": "action2"}, {...}, {...}, {...}]; Var ParseMyResultsArray = function () {console.log (AllResults); // ...}; Angular.forEach ($ scope.requests, function (request) {var promise = $ http.get (request.uri); promises.push (promise);}); Angular.forEach (Promise, Work (Promise) {Promise. Then (Work (Results) {AllResults.push (Results);}}}}; // Then, when all the promises are resolved ... ParseMyResultsArray ();   

How can I be sure to call ParseMyResultsArray () when my promises to know the size of my requests Is it sorted without a > array?

Thank you for your help!

From angle to docs:

$ http API $ q Based on service deferred / wired API.

So it is possible to use the method that takes an array of promises and:

combines many promises in the same promise, all the promises Solution is resolved

The implementation code is:

  $ q.all (promises). Then (work () (/ * all promises are solved * /});    

No comments:

Post a Comment