Axe*_*era 2 google-maps-api-3 node.js async-await
The following is example taken from the Google Maps node module
function doSomeGeoCode() {
const googleMapsClient = require('@google/maps').createClient({
key: 'your API key here',
Promise: Promise
});
googleMapsClient.geocode({address: '1600 Amphitheatre Parkway, Mountain
View, CA'})
.asPromise()
.then((response) => {
console.log(response.json.results);
})
.catch((err) => {
console.log(err);
});
}
Run Code Online (Sandbox Code Playgroud)
如何使用async和await调用doSomeGeoCode。同样,一旦收到响应,我就需要调用另一个函数。请建议
您只需要返回承诺googleMapsClient并创建另一个方法来等待响应,例如:
function doSomeGeoCode() {
const googleMapsClient = require('@google/maps').createClient({
key: 'your API key here',
Promise: Promise,
});
// Return the promise
return googleMapsClient.geocode({
address: '1600 Amphitheater Parkway, Mountain View,CA ',
})
.asPromise();
}
async function myTest() {
try {
// Called the method which returns promise.
// `await` will wait to get promise resolved.
const result = await doSomeGeoCode();
console.log(result);
} catch (error) {
// If promise got rejected.
console.log(error);
}
}
myTest();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1176 次 |
| 最近记录: |