0
我需要找到一种方式返回一个字符串来消化我的主代码块以及一个回调或开始处理我的主代码块中的其余代码一旦值摘要被返回。NodeJS回调和返回值混淆
请帮忙!
这是我目前的代码不起作用。
var digest = checkIntegrity(filePath, res[3]);
//digest always come back undefined and never matches res[2] so file always deletes
if (digest === 0){
console.log('File Inaccessible');
} else {
if (digest === res[2]){
createNewFile();
} else {
console.log('File hash doesn't match');
delBadFile();
}
}
function checkIntegrity(filePath, algorithm, cb){
console.log('in checkIntegrity');
var hash = crypto.createHash(algorithm);
var digest;
//see if file is there
fs.stat(filePath, function(fileErr, fileStats){
if(fileErr){
//error accessing file, most likely file does not exist
return 0;
} else {
//file exists
var fileIn = fs.createReadStream(filePath);
fileIn.on('data', function(chunk){
if (chunk) {
hash.update(chunk);
}
});
fileIn.on('end', function(){
return hash.digest('hex');
});
}
});
}