Firstly I know there is already a lot of stuff about async on here, but I have looked in many places on and off SO and not found anything which seems to answer this particular question, at least not to the untrained eyes of this noob.
Background
I have a function foo
which depends on the result of another function googlePlaces
which contains a promise, but the overall function is not a promise.
JS
var googleMapsClient = require('@google/maps').createClient({
key: <API_KEY>
Promise: Promise
});
...
function foo() {
var point = [49.215369,2.627365]
var results = googlePlaces(point)
console.log('line 69')
console.log(results)
// do some more stuff with 'results'
}
function googlePlaces(point) {
var placesOfInterest = [];
var latLng = (point[0]+','+point[1])
var request = {
location: latLng,
radius: 10000
};
googleMapsClient.placesNearby(request).asPromise()
.then(function(response){
placesOfInterest.push(response.json.results)
})
.finally(function(){
console.log('end of googlePlaces function:')
console.log(placesOfInterest);
return(placesOfInterest);
})
}
console log:
line 69
undefined
end of googlePlaces function
[my lovely array]
What I've tried
I've tried making foo
an async function, and changing results
to be = await googlePlaces(point)
, but I still get undefined, and also it throws UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
I can move everything from foo
after line 69 to be within the googlePlaces
function, and that works, but for neatness and readability of code it would be much better if it could stay in foo