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);
})
});
}
添加回答
舉報