使用回调响应作为另一个函数参数

Use callback response as another function argument

我正在使用地理编码器 API,它 returns 导致回调

data = data ? data : {} // if not given initialize to empty
geocoder.geocode('Kathmandu, Nepal' , function(err, res) {
data.lat = res[0].latitude;
data.lng = res[0].longitude;
});

并使用数据object构建地理点

here = new GeoPoint(here); // data is still {} here

我的问题是如何在上述场景中使用回调的响应?

geocode 方法是异步的 - 因此您要对响应进行的任何工作都需要在回调中进行:

geocoder.geocode('Kathmandu, Nepal' , function(err, res) {
    data.lat = res[0].latitude;
    data.lng = res[0].longitude;

    here = new GeoPoint(here); //HAS TO BE IN A CALLBACK 
    //- or passed to another function from within the callback
});