Thursday 20 April 2017

Promise in for loop returns wrong value

This might be a silly question. Below is a stripped down version of my code.

for (var i = 0; i < objects.length; i++) { // objects.length = 10
    CuQuery.count().then(function(result){
      Cucount = result;
      console.log("i value = "+i);
    }, function(error) {
        response.error(error);
    });
}

In console, I get i value = 10, 10 times as output. I expected it to print value 1 to 10. What am I doing wrong ? or Is it the correct behavior ?



via Sajan

No comments:

Post a Comment