简体   繁体   中英

How to await the result of a call to a function which is NOT a promise, but contains a promise?

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) 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

In googlePlaces() do:

return googleMapsClient.placesNearby(request).asPromise();

then in foo() do:

async function foo(){
  ...    
  var results = await googlePlaces(point);
  results.then( /* do you then stuff here */);
}

If you are sure that foo returns a promise, Promisfy it to chain with then:

function foo() {

  var point = [49.215369,2.627365]
  var results = googlePlaces(point)
   return results;
 }

(new Promise(function(res){
     res(foo());
})).then(function(result){
//do something with result..
})

Without having to change your code too much you can wrap the google places promise within another promise that bubbles up to foo(). From there you can handle the results.

function foo() {

    var point = [49.215369,2.627365]
    var promise = googlePlaces(point)

    promise.then((results) => {
        // do stuff with 'results'
        console.log(results)
    });

}

function googlePlaces(point) {

    var placesOfInterest = [];

    var latLng = (point[0]+','+point[1])
    var request = {
      location: latLng,
      radius: 10000
    };

    return new Promise((resolve) => {
        googleMapsClient.placesNearby(request).asPromise();
        .then(function(response){        
           placesOfInterest.push(response.json.results)      
           }) 

        .finally(function(){
           console.log('end of googlePlaces function:')
           console.log(placesOfInterest);
           // resolve the promise 
           resolve(placesOfInterest);
           })
    });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM