For() loop go back at begining after few iterations

Multi tool use
Multi tool use


For() loop go back at begining after few iterations



I have a very basic



for(int i=0; i<array.length; i++) loop in an ExpressJs application. It iterates to access an array elements, do an http request, wait for the answer (with await), than save the results in a mongodb database.
After a few iterations (3 or 4 elements) i can see in my logs that the i variable go back to 0 and it seems that the loop begins from 0 again; or my array have more than 100 elements.


for(int i=0; i<array.length; i++)



Desired behaviour : iterate on all elements, only one time inside my loop, from element 0 to array.length (basicaly i'm looking for normal behaviour of for() loop)
Why??



Complete code :


for(var i =0; i < stockList.length; i++){

var urlapi = someURL;



let A = await getStock(urlapi);



var symbolOfreturn = A.data["Meta Data"]["2. Symbol"];

var lastClose = A.data["Time Series (Daily)"]["2018-06-29"]["4. close"];

var openToday = A.data["Time Series (Daily)"]["2018-06-29"]["1. open"];

var stock = new Stock({

Symbol: symbolOfreturn,

Market: "Nasdaq",

Sector: stockListTechnologie[i]["Sector"],

Name: stockListTechnologie[i]["Name"],

LastClose: lastClose,

OpenToday: openToday,

MarketCap: stockListTechnologie[i]["MarketCap"]

});



stock.save(function (err, stock) {



if (err) return console.error(err);

console.log(stock);

//201 for created

});


//timer to delay every http request
var stop = new Date().getTime();

while(new Date().getTime() < stop + 10000) {

;

};

//HERE IS THE ERROR, the i variable goes back to 0 after 3 or 4 iterations
console.log("value of: " + i);



};





Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself.
– LGSon
Jul 1 at 15:01





Strongly refer to How do I ask a good question? in the SE Help Center.
– Alex
Jul 1 at 15:07


How do I ask a good question?


SE Help Center





I added my desired behaviour, the problem is very specific, i suspect that the code doesn't iterate properly because of the way of node.js try to optimize the execution. I'm more seeking a confirmation here.
– Fazil.M
Jul 1 at 15:07





You most likely have a bug in your code which has nothing to do with Node. If you can share the contents of the loop, that would be helpful.
– Damon
Jul 1 at 15:14





Eeeck. This while(new Date().getTime() < stop + 10000) {;}; is horrible.
– jfriend00
Jul 1 at 15:55


while(new Date().getTime() < stop + 10000) {;};









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

JcnASYV3Obq,3 gSIXBxxj4DJRAbD0932tCTH
aEo0 ekeHY 7rdk8f25 pHl PLB,YuRwq,doLSttaT8HkWmy,PpLHWk7qH en4d vWIN9eZrbLPzt9IDFEdz7A

Popular posts from this blog

Rothschild family

Cinema of Italy