3 回答
TA贡献1809条经验 获得超8个赞
在googlePlaces()做:
return googleMapsClient.placesNearby(request).asPromise();
然后在foo()做:
async function foo(){
...
var results = await googlePlaces(point);
results.then( /* do you then stuff here */);
}
TA贡献1998条经验 获得超6个赞
如果您确定 foo 返回一个承诺,请承诺它与 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..
})
TA贡献1921条经验 获得超9个赞
无需过多更改代码,您就可以将 google 位置承诺包装在另一个冒泡到 foo() 的承诺中。从那里你可以处理结果。
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);
})
});
}
添加回答
举报