如何等待对不是承诺但包含承诺的函数的调用结果?

首先,我知道这里已经有很多关于异步的东西,但是我在很多地方都看过了,但没有找到任何似乎可以回答这个特定问题的东西,至少对于这个菜鸟未经训练的眼睛来说是这样。


背景

我有一个函数foo依赖于另一个函数的结果,googlePlaces其中包含了一个承诺,但整体功能是不是一个承诺。


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

       })

    

}


控制台日志:

第 69 行

未定义

的 googlePlaces 函数结束

[我可爱的数组]


我试过的

我试过创建foo一个异步函数,并更改results为= await googlePlaces(point),但我仍然未定义,并且它抛出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)


我可以将foo第 69 行之后的所有内容都移到googlePlaces函数内,这可行,但是为了代码的整洁性和可读性,如果它可以保留在foo



肥皂起泡泡
浏览 159回答 3
3回答

海绵宝宝撒

在googlePlaces()做:return googleMapsClient.placesNearby(request).asPromise();然后在foo()做:async function foo(){&nbsp; ...&nbsp; &nbsp;&nbsp;&nbsp; var results = await googlePlaces(point);&nbsp; results.then( /* do you then stuff here */);}

米琪卡哇伊

如果您确定 foo 返回一个承诺,请承诺它与 then 链接:function foo() {&nbsp; var point = [49.215369,2.627365]&nbsp; var results = googlePlaces(point)&nbsp; &nbsp;return results;&nbsp;}(new Promise(function(res){&nbsp; &nbsp; &nbsp;res(foo());})).then(function(result){//do something with result..})

郎朗坤

无需过多更改代码,您就可以将 google 位置承诺包装在另一个冒泡到 foo() 的承诺中。从那里你可以处理结果。function foo() {&nbsp; &nbsp; var point = [49.215369,2.627365]&nbsp; &nbsp; var promise = googlePlaces(point)&nbsp; &nbsp; promise.then((results) => {&nbsp; &nbsp; &nbsp; &nbsp; // do stuff with 'results'&nbsp; &nbsp; &nbsp; &nbsp; console.log(results)&nbsp; &nbsp; });}function googlePlaces(point) {&nbsp; &nbsp; var placesOfInterest = [];&nbsp; &nbsp; var latLng = (point[0]+','+point[1])&nbsp; &nbsp; var request = {&nbsp; &nbsp; &nbsp; location: latLng,&nbsp; &nbsp; &nbsp; radius: 10000&nbsp; &nbsp; };&nbsp; &nbsp; return new Promise((resolve) => {&nbsp; &nbsp; &nbsp; &nbsp; googleMapsClient.placesNearby(request).asPromise();&nbsp; &nbsp; &nbsp; &nbsp; .then(function(response){&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;placesOfInterest.push(response.json.results)&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;})&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; .finally(function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log('end of googlePlaces function:')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;console.log(placesOfInterest);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// resolve the promise&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;resolve(placesOfInterest);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;})&nbsp; &nbsp; });}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript