I am trying to resolve this issue which is related to http requests.
I need to fire multiple http request inside a for loop. The for loop provides the url for the request.
I also need to push project id
to an array
based from the loop variable.
var array = [];
for(var i = 0; i < project.length; i++) {
var url = project[i].url
getProjectDetail(url)
.then(function(data) {
console.log(i)
//this only outputs project.length which is 10
array.push ({id:project[i].id, detail:data.detail})
// error here Cannot read property 'id' of undefined
}
}
It seem like the request takes time and variable i
is always the max length. I am not sure how to resolve this issue. Any thoughts?
Thanks so much!