request({url: API_URL}, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log("Items received with success.");
var data = JSON.parse(body);
// catch duration from video
for(var list in data) {
var item = data[list];
async.series([
function(callback){
console.log("I'm inside fileExists function");
var result = '';
request.get('http://tobeymotos.com.br/video.mp4', function (error, response, body) {
if (!error && response.statusCode == 200) {
var video = body;
console.log("File exists and is OK");
var result = true;
console.log("Valor da result", result);
callback(null, result);
}
else{
console.log("This file do not exists, check error: "+ error);
var result = "xixi";
callback(null, result);
}
});
// usar aqui
},
function(callback){
// catch video duration
file = "http://tobeymotos.com.br/video.mp4"; // used as e.g.
console.log("I'm inside getVideoDuration function");
getDuration(file).then(function (duration) {
console.log("Duração do vÃdeo: " +duration);
return duration;
});
callback(null, 'b');
},
function(callback){
// code c
callback(null, 'c');
},
function(callback){
// code d
callback(null, 'd');
}],
// optional callback
function(err, results){
console.log(results);
}
)
return 0;
}
} else {
console.log("Error: "+error);
}
});
您的回调超出了请求范围。get函数,由于Node JS的性质,这可能会导致回调在请求返回之前调用。Node JS是非阻塞的,因此在转移到下一个函数之前不会等待函数被执行,除非明确告知。将回调放在请求中。get响应函数将强制它等待请求返回,然后使用结果执行回调