how to access response value within a function in for loop

  for-loop, javascript, settimeout

can someone please help me out, I have a for loop inside that there’s a setTimout the call’s the server after every second and the server responds with a value but when I try accessing the server response I get undefined. here is my code:

for (let i = 0; i <= latlng.length; i++) {
  setTimeout(function() {
    GeoCoder.geocode({
      'address': latlng[i],
    }, function(results, status) {
      if (results) {
        var lng = results[0].geometry.location.lng()
        var lat = results[0].geometry.location.lat()
      }
      console.log("are we getting location", lng + "" + lat)
    });
  }, i * 250);
}

Source: Ask Javascript Questions

LEAVE A COMMENT