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

标签 javascript md5 filereader

我尝试使用 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/

相关文章:

javascript - 失败 : Build failed with an exception. - React Native

MySQL ENCRYPT 字段转 MD5

c - C 中 XML 文件的 MD5 实现

javascript - 等待文件加载(onload JavaScript)

javascript - 滚动后全屏 ImageView 无法正常运行

javascript - 无法读取 gulp 中未定义的属性 'watch'

javascript - 如何使输入成为 View 的默认输入字段?

javascript - 抓取需要 md5 哈希作为参数的网页

reactjs - 如何在reactJS中显示从输入类型=文件中选择的图像

html - 内网IE兼容模式和Filereader的问题