gpt4 book ai didi

javascript - filereader,使用 SparkMD5 检查图像的校验和,promises

转载 作者:行者123 更新时间:2023-11-30 19:08:37 26 4
gpt4 key购买 nike

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

countMD5Hash = function(data){
return new Promise ((resolve,reject) => {
let res = null;

res = SparkMD5.ArrayBuffer.hash(data)

if(res){
resolve(res)
}
});
};

当我尝试像上面那样调用 countMD5Hash 时,它返回未定义,但是当我尝试在这个函数中 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
}

关于javascript - filereader,使用 SparkMD5 检查图像的校验和,promises,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58729929/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com