filereader,使用SparkMD5检查图像的校验和,承诺



我正试图用SparkMD5库计算校验和,我用FileReader从文件中正确读取了ArrayBuffer,并将ArrayBuff传递给我的新函数:

countMD5Hash = function(data){
return new Promise ((resolve,reject) => {
let res = null;
res = SparkMD5.ArrayBuffer.hash(data)
if(res){
resolve(res)
}
});
};

当我试图像上面那样调用countMD5Hash时,它会返回undefined,但当我试图在没有解析的情况下console.log函数时,它记录正确的校验和。如何使用计数的校验和来解析响应?

解析promise不是返回值。要使用resovled值,您应该像这样使用then

countMD5Hash(data).then(result => {
// do whatever you want with the result
})

但是,如果您在async函数中使用countMD5Hash,您也可以像这样使用await

let handle = async function(data) {
// this waits for the promise to fulfill (or reject! don't forget to deal with exceptions!)
// and puts the resolved value into hash
let hash = await countMD5Hash(data); 
// do whatever you want with hash
}

最新更新