[英]async function returns “undefined”
我有一个异步函数,应该返回一个地理编码值:
async function latlng(place){
//var str;
return googleMapsClient.geocode({
address: place
}).asPromise()
.then((response) => { response.json.results[0].geometry.location
/*str = response.json.results[0].geometry.location;
return str;*/
})
.catch((err) => {
console.log(err);
});
}
当我调用它时,它什么也不返回,但是它有一个值
我打电话:
(async function(){
//location start
start = await data.latlng(req.body.start);
//location end
end = await data.latlng(req.body.end);
})();
如果在函数中一切正常,为什么不返回任何内容? 我怎么解决这个问题?
您正在使用async / await
..很好,但是由于您的函数中的某些原因,您决定不打扰.. :)
同样,在latlng函数中捕获错误也没有意义,因为您的start
/ end
肯定要求两者都有效才有意义。
这是简化的latlng函数,它使用async / await
来实现其目的。
async function latlng(place){
const response =
await googleMapsClient.geocode({ address: place }).asPromise();
return response.json.results[0].geometry.location;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.