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');
});
}
});
}