Can someone explain to me what is happening to the for
loop here? I don't understand why the loop goes beyond its' condition. I expect condition [i]
to stop at '2'. I suppose that this behavior would be same for other async functions inside a for
loop. Much thanks!
var path = require('path')
var fs = require('fs')
var request = require('request')
var cheerio = require('cheerio')
for (i=0; i < 3; i++) {
console.log(i)
var arr = []
var url = 'https://en.wikipedia.org/wiki/Web_scraping'
request (url, function(error,response,body) {
if(error){
throw err;
} $ = cheerio.load(body)
var x = $.html()
console.log(i)
})
}
/* Results
0
1
2
3
3
3
*/