javascript - Running $http requests sequentially in AngularJS -
I have items on my $ scope
for all those items, give me three $ http requests I need to run. These requests should be run in a special order, even if there is no failure. I'm not sure how to do it in a beautiful way, with the paradigm of promise. I have a lot of duplicate code and it looks really confusing. I have to do this wrong at present, I have the following:
$ scope.items = getItems (); $ Scope.currentIndex = 0; $ Scope.executeItem = function () {$ http.get ($ scope.items [$ scope.currentIndex] .urlA) .then (function (resA) {$ scope.items [$ scope.currentIndex] .urlAWorks = true; $ Http.get ($ scope.items [$ scope.currentIndex] .urlB) .then (function (resB) {$ scope.items [$ scope.currentIndex] .urlBWorks = true; $ http.get ($ scope.items ) [$ Scope.currentIndex] .urlC) .then (function (resC) {$ scope.items [$ scope.currentIndex] .urlCWorks = true; $ scope.currentIndex = $ scope.currentIndex + 1; $ scope.executeItem ( );}, function (errC) {$ scope.items [$ scope.currentIndex] .urlCWorks = false; $ scope.currentIndex = $ scope.currentIndex + 1; $ scope.executeItem ();})}, function (errB ) {$ Scope.items [$ scope.currentIndex] .urlBWorks = false;})}}, function (ARAA) {$ scope.items [$ scope.current index] .urlAWorks = false; $ Http.get ($ scope.items [$ scope.currentIndex] .urlB) .then (function (resB) {$ scope.items [$ scope.currentIndex] .urlBWorks = true; $ http.get ($ scope.items ) [$ Scope.currentIndex] .urlC) .then (function (resC) {$ scope.items [$ scope.currentIndex] .urlCWorks = true; $ scope.currentIndex = $ scope.currentIndex + 1; $ scope.executeItem ( );}, function (errC) {$ scope.items [$ scope.currentIndex] .urlCWorks = false; $ scope.currentIndex = $ scope.currentIndex + 1; $ scope.executeItem ();})}, function (errB ) {$ Scope.items [$ scope.currentIndex] .urlBWorks = false;}); }); };
Do I really promise it? It looks off the road.
Thanks
Promise you are promising :) Since Then
gives a promise, you can:
$ http.get (urlA). Then (function (dataA) {dustfewtha (dataA); return $ http .get (urlB);}). Then return to the function (databi) {DustFollow (databi); $ http.get (urlC);}) then (function (datacity) {dustfuth c (datacy); return true;})
Comments
Post a Comment