请问怎么在主调方法内获取异步请求的回调函数的返回值?如下:
var csvpath = "C:/Users/name/Desktop/address.csv";
var requrl = 'http://api.map.baidu.com/geocoder/v2/?ak=xxxxxxxxx&output=json&pois=1&location=';
//需求:csv读取每行,异步请求获取返回结果,结果输出到result.csv
csv().from.path(csvpath, {header: true, columns: true})
.transform(function(row,index){
if(index < 2) return;
if(!row){
throw new Error('error on import row index #'+index+' '+JSON.stringify(row));
}
var i = row.length;
var location = row.lat.trim() + ',' + row.lng.trim();
//异步请求,在回调函数中处理结果返回,怎么在外面获取这个返回值???
//transform内部需要return一个结果,输出到result.csv中
//想实现请求时阻塞直到返回结果,并在transform中返回,请问怎么做呢?
//一直对异步和回调理解不够
request.get({url:requrl+location}, function (error, response, body) {
if(!error && response.statusCode == 200 && body.status == 0) {
var resString = '';
body = JSON.stringify(body);
resString = index + "," + body.location.lat + "," + body.location.lng + ","
+ body.formatted_address + "," + body.business + "," + body.addressComponent.city + ","
+ body.addressComponent.district + "," + body.addressComponent.province + ","
+ body.addressComponent.street + "," + body.addressComponent.street_number;
var pois = body.pois;
for(var i=0; i<pois.length; i++){
var poi = pois[i];
resString += "," + poi.name + "," + poi.poiType + "," + poi.addr + ","
+ poi.point.x + "," + poi.point.y + "," + poi.distance;
}
return resString;
}else{
console.log(index + "--- " + error + "---" + response.statusCode);
//console.log(index + " error : " + body);
return index + ", error ";
}
});
console.log(res);
return res;
})
.on('end', function(count){
console.log('Number of lines: '+count);
})
.on('error', function(error){
console.log(error);
return next(error);
})
.to.path('./result.csv');
5 回复
怎么没有人回答一下呢
求解决办法
帮你顶一个~~
用csv transform 异步模式可以不考虑上面那个问题了,下面是代码,但是下面代码另一个问题:如果在回调中获取改行对应的index,由于是异步,获取的index是错误的,不知如何解决,网大家指教
csv().from.path(csvpath, {header: true, columns: true})
.transform(function(row,index, callback){
if(index < 2) return;
if(!row){
throw new Error('error on import row index #'+index+' '+JSON.stringify(row));
}
var location = row.lat.trim() + ',' + row.lng.trim();
var row_index = index;
request.get({url:requrl+location}, function (error, response, body) {
var resString = '';
if(!error && response.statusCode == 200) {
body = JSON.parse(body);
if(body.status == '0'){
var result = body.result;
resString = row_index + ',' + result.location.lat + ',' + result.location.lng + ',"'
+ result.formatted_address + '","' + result.business + '",' + result.addressComponent.city + ','
+ result.addressComponent.district + ',' + result.addressComponent.province + ',"'
+ result.addressComponent.street + '",' + result.addressComponent.street_number;
var pois = result.pois;
for(var i=0; i<pois.length; i++){
var poi = pois[i];
resString = resString + ',poi_' + i + ',"' + poi.name + '","' + poi.poiType + '","' + poi.addr + '",'
+ poi.point.x + ',' + poi.point.y + ',' + poi.distance;
}
}else{
resString = row_index + ", error \n";
}
}else{
console.log(row_index + "--- " + error + "---" + response.statusCode + '-----body--' + body);
resString = row_index + ", error \n";
}
callback(null, resString + "\n");
});
})
.on('end', function(count){
console.log('Number of lines: '+count);
})
.on('error', function(error){
console.log(error);
return next(error);
})
.to.path('./result_04.csv');
//异步请求,在回调函数中处理结果返回,怎么在外面获取这个返回值??? 可以在回调函数中调用外部过程;可以触发一个事件,另有外部过程订阅该事件。