Sequential loop, AngularJS Promises [duplicate]












0















This question already has an answer here:




  • JavaScript closure inside loops – simple practical example

    39 answers




I am trying to figure out how to resolve an arbitrary number of promises one after another within a for-loop.



I have a function called scrapePage(num) that takes in a number and makes an HTTP call to a certain web-page.
I have tried out a number of approaches to this problem.



1) Use an array of promises (that resolves them in random order):



var promises = ;
for (var i = 0; i < 4; i++) {
promises.push(scrapPage(i));
}
$q.all(promises).then(doMoreThings);


2) Use a chain of calls (that is useless for an arbitrary number of calls):



var chain = $q.when();
chain.then(function() {
return scrapPage(0);
}).then(function() {
return scrapPage(1);
}).then(function() {
return scrapPage(2);
}).then(function() {
return scrapPage(3);
}).then(doMoreThings);


I was playing around with the chain function, trying to get it to work. For testing purposes, I made scrapePage print out "page #n" every time it resolves.



Here's what I have right now:



var chain = $q.when();
for (var i = 0; i < 4; i++) {
console.log("i: " + i);
chain = chain.then(function() {
console.log("ni: " + i);
return scrapPage(i);
});
}


I can point out two major problems it has.



First, its output is absolutely weird.



i: 0
i: 1
i: 2
i: 3

i: 4
page #4

i: 4
page #4

i: 4
page #4

i: 4
page #4


Second, I don't know how to make it doMoreThings after all promises are resolved.



Could anyone tell me what I'm missing?










share|improve this question















marked as duplicate by georgeawg angularjs
Users with the  angularjs badge can single-handedly close angularjs questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 21 at 12:06


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















    0















    This question already has an answer here:




    • JavaScript closure inside loops – simple practical example

      39 answers




    I am trying to figure out how to resolve an arbitrary number of promises one after another within a for-loop.



    I have a function called scrapePage(num) that takes in a number and makes an HTTP call to a certain web-page.
    I have tried out a number of approaches to this problem.



    1) Use an array of promises (that resolves them in random order):



    var promises = ;
    for (var i = 0; i < 4; i++) {
    promises.push(scrapPage(i));
    }
    $q.all(promises).then(doMoreThings);


    2) Use a chain of calls (that is useless for an arbitrary number of calls):



    var chain = $q.when();
    chain.then(function() {
    return scrapPage(0);
    }).then(function() {
    return scrapPage(1);
    }).then(function() {
    return scrapPage(2);
    }).then(function() {
    return scrapPage(3);
    }).then(doMoreThings);


    I was playing around with the chain function, trying to get it to work. For testing purposes, I made scrapePage print out "page #n" every time it resolves.



    Here's what I have right now:



    var chain = $q.when();
    for (var i = 0; i < 4; i++) {
    console.log("i: " + i);
    chain = chain.then(function() {
    console.log("ni: " + i);
    return scrapPage(i);
    });
    }


    I can point out two major problems it has.



    First, its output is absolutely weird.



    i: 0
    i: 1
    i: 2
    i: 3

    i: 4
    page #4

    i: 4
    page #4

    i: 4
    page #4

    i: 4
    page #4


    Second, I don't know how to make it doMoreThings after all promises are resolved.



    Could anyone tell me what I'm missing?










    share|improve this question















    marked as duplicate by georgeawg angularjs
    Users with the  angularjs badge can single-handedly close angularjs questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 21 at 12:06


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















      0












      0








      0








      This question already has an answer here:




      • JavaScript closure inside loops – simple practical example

        39 answers




      I am trying to figure out how to resolve an arbitrary number of promises one after another within a for-loop.



      I have a function called scrapePage(num) that takes in a number and makes an HTTP call to a certain web-page.
      I have tried out a number of approaches to this problem.



      1) Use an array of promises (that resolves them in random order):



      var promises = ;
      for (var i = 0; i < 4; i++) {
      promises.push(scrapPage(i));
      }
      $q.all(promises).then(doMoreThings);


      2) Use a chain of calls (that is useless for an arbitrary number of calls):



      var chain = $q.when();
      chain.then(function() {
      return scrapPage(0);
      }).then(function() {
      return scrapPage(1);
      }).then(function() {
      return scrapPage(2);
      }).then(function() {
      return scrapPage(3);
      }).then(doMoreThings);


      I was playing around with the chain function, trying to get it to work. For testing purposes, I made scrapePage print out "page #n" every time it resolves.



      Here's what I have right now:



      var chain = $q.when();
      for (var i = 0; i < 4; i++) {
      console.log("i: " + i);
      chain = chain.then(function() {
      console.log("ni: " + i);
      return scrapPage(i);
      });
      }


      I can point out two major problems it has.



      First, its output is absolutely weird.



      i: 0
      i: 1
      i: 2
      i: 3

      i: 4
      page #4

      i: 4
      page #4

      i: 4
      page #4

      i: 4
      page #4


      Second, I don't know how to make it doMoreThings after all promises are resolved.



      Could anyone tell me what I'm missing?










      share|improve this question
















      This question already has an answer here:




      • JavaScript closure inside loops – simple practical example

        39 answers




      I am trying to figure out how to resolve an arbitrary number of promises one after another within a for-loop.



      I have a function called scrapePage(num) that takes in a number and makes an HTTP call to a certain web-page.
      I have tried out a number of approaches to this problem.



      1) Use an array of promises (that resolves them in random order):



      var promises = ;
      for (var i = 0; i < 4; i++) {
      promises.push(scrapPage(i));
      }
      $q.all(promises).then(doMoreThings);


      2) Use a chain of calls (that is useless for an arbitrary number of calls):



      var chain = $q.when();
      chain.then(function() {
      return scrapPage(0);
      }).then(function() {
      return scrapPage(1);
      }).then(function() {
      return scrapPage(2);
      }).then(function() {
      return scrapPage(3);
      }).then(doMoreThings);


      I was playing around with the chain function, trying to get it to work. For testing purposes, I made scrapePage print out "page #n" every time it resolves.



      Here's what I have right now:



      var chain = $q.when();
      for (var i = 0; i < 4; i++) {
      console.log("i: " + i);
      chain = chain.then(function() {
      console.log("ni: " + i);
      return scrapPage(i);
      });
      }


      I can point out two major problems it has.



      First, its output is absolutely weird.



      i: 0
      i: 1
      i: 2
      i: 3

      i: 4
      page #4

      i: 4
      page #4

      i: 4
      page #4

      i: 4
      page #4


      Second, I don't know how to make it doMoreThings after all promises are resolved.



      Could anyone tell me what I'm missing?





      This question already has an answer here:




      • JavaScript closure inside loops – simple practical example

        39 answers








      javascript angularjs promise angular-promise






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 21 at 12:06









      georgeawg

      32.6k104967




      32.6k104967










      asked Nov 21 at 7:57









      rutilum

      31




      31




      marked as duplicate by georgeawg angularjs
      Users with the  angularjs badge can single-handedly close angularjs questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 21 at 12:06


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by georgeawg angularjs
      Users with the  angularjs badge can single-handedly close angularjs questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 21 at 12:06


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          0














          Try changing var to let.



          var chain = $q.when();
          for (let i = 0; i < 4; i++) {
          console.log("i: " + i);
          chain = chain.then(function() {
          console.log("ni: " + i);
          return scrapPage(i);
          });
          }


          That would probably solve the first problem.



          For the second one, you could add a condition like this:



          if (i == 4){
          doMoreThings();
          }
          else {
          return scrapPage(i);
          }


          instead of just return scrapPage(i);






          share|improve this answer





















          • Such an easy fix! Thanks a lot, it worked out perfectly.
            – rutilum
            Nov 21 at 8:23


















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Try changing var to let.



          var chain = $q.when();
          for (let i = 0; i < 4; i++) {
          console.log("i: " + i);
          chain = chain.then(function() {
          console.log("ni: " + i);
          return scrapPage(i);
          });
          }


          That would probably solve the first problem.



          For the second one, you could add a condition like this:



          if (i == 4){
          doMoreThings();
          }
          else {
          return scrapPage(i);
          }


          instead of just return scrapPage(i);






          share|improve this answer





















          • Such an easy fix! Thanks a lot, it worked out perfectly.
            – rutilum
            Nov 21 at 8:23
















          0














          Try changing var to let.



          var chain = $q.when();
          for (let i = 0; i < 4; i++) {
          console.log("i: " + i);
          chain = chain.then(function() {
          console.log("ni: " + i);
          return scrapPage(i);
          });
          }


          That would probably solve the first problem.



          For the second one, you could add a condition like this:



          if (i == 4){
          doMoreThings();
          }
          else {
          return scrapPage(i);
          }


          instead of just return scrapPage(i);






          share|improve this answer





















          • Such an easy fix! Thanks a lot, it worked out perfectly.
            – rutilum
            Nov 21 at 8:23














          0












          0








          0






          Try changing var to let.



          var chain = $q.when();
          for (let i = 0; i < 4; i++) {
          console.log("i: " + i);
          chain = chain.then(function() {
          console.log("ni: " + i);
          return scrapPage(i);
          });
          }


          That would probably solve the first problem.



          For the second one, you could add a condition like this:



          if (i == 4){
          doMoreThings();
          }
          else {
          return scrapPage(i);
          }


          instead of just return scrapPage(i);






          share|improve this answer












          Try changing var to let.



          var chain = $q.when();
          for (let i = 0; i < 4; i++) {
          console.log("i: " + i);
          chain = chain.then(function() {
          console.log("ni: " + i);
          return scrapPage(i);
          });
          }


          That would probably solve the first problem.



          For the second one, you could add a condition like this:



          if (i == 4){
          doMoreThings();
          }
          else {
          return scrapPage(i);
          }


          instead of just return scrapPage(i);







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 at 8:06









          holydragon

          1,2331618




          1,2331618












          • Such an easy fix! Thanks a lot, it worked out perfectly.
            – rutilum
            Nov 21 at 8:23


















          • Such an easy fix! Thanks a lot, it worked out perfectly.
            – rutilum
            Nov 21 at 8:23
















          Such an easy fix! Thanks a lot, it worked out perfectly.
          – rutilum
          Nov 21 at 8:23




          Such an easy fix! Thanks a lot, it worked out perfectly.
          – rutilum
          Nov 21 at 8:23



          Popular posts from this blog

          Feedback on college project

          Albești (Vaslui)

          Futebolista