我知道我需要在这里使用回调,但到目前为止所有尝试都失败了。我想console.log记录true或false。先感谢您。
var req_fn = function () {
request(url, function(error, response, html){
if(!error){
parseString(response.body ,function (err, result){
var raw_json = result;
var current_zip = get_current_zip(raw_json);
if (current_zip.toString() === pack.creator_zip.toString() ) {
return true; // would like to return this value
} else {
return false; // or this one
}
});
}
});
};
console.log(req_fn());
答案 0 :(得分:1)
这里是该函数中回调的样子
var req_fn = function (callback) {
request(url, function (error, response, html) {
if (err) {
callback(err, null);
}else {
parseString(response.body, function (err, result) {
if (err) {
callback(err, null);
} else {
var raw_json = result;
var current_zip = get_current_zip(raw_json);
var result = current_zip.toString() === pack.creator_zip.toString();
callback(null, result);
}
});
}
});
};
req_fn(function(err, result))
if (!err) console.log(result);
});