Ad
How To Handle Multi Google Places API Request Inside Loop In NodeJS?
I have a loop that analyse long array of GPS points, then I pick some points according to what I need.
For each GPS point I want to find places around.
How to make sure to get each response separated from others?
Here is the code inside the loop, it works when I have 1 GPS point, but with more it's not:
Loop the GPS path, saved in hash table:
for (let indexI = 0; indexI < path_hash.length; indexI++) {
for (let indexJ = 0; indexJ < path_hash[indexI].length - 2; indexJ++) {
...
Prepare the URL request:
location = path_hash[indexI][indexJ].data.coords.latitude + "," + path_hash[indexI][indexJ].data.coords.longitude;
var url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?" + "key=" + key + "&location=" + location + "&radius=" + radius + "&sensor=" + sensor + "&types=" + types + "&keyword=" + keyword;
...
Execute the request:
https.get(url, function (response) {
var body = '';
response.on('data', function (chunk) {
body += chunk;
});
response.on('end', function () {
var places = places + JSON.parse(body);
var locations = places.results;
console.log(locations);
});
}).on('error', function (e) {
console.log("Got error: " + e.message);
})
Ad
Answer
Using your function you can do it this way
// Turn the callback function into a Promise
const fetchUrl = (url) => {
return new Promise((resolve, reject) => {
https.get(url, function (response) {
var body = '';
response.on('data', function (chunk) {
body += chunk;
});
response.on('end', function () {
var places = places + JSON.parse(body);
var locations = places.results;
resolve(locations) // locations is returned by the Promise
});
}).on('error', function (e) {
console.log("Got error: " + e.message);
reject(e); // Something went wrong, reject the Promise
});
});
}
// Loop the GPS path, saved in hash table
...
// Prepare the urls
...
const GPSPoints = [
'url1',
'url2',
...
];
// Fetch the locations for all the GPS points
const promises = GPSPoints.map(point => fetchUrl(point));
// Execute the then section when all the Promises have resolved
// which is when all the locations have been retrieved from google API
Promise.all(promises).then(all_locations => {
console.log(all_locations[0]); // Contains locations for url1
console.log(all_locations[1]); // Contains locations for url2
...
});
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad