javascript - 文件阅读器,使用 SparkMD5 检查图像的校验和,承诺
问题描述
我试图用 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 而不解析时,它会记录正确的校验和。如何执行该功能将通过计数校验和解决响应?
解决方案
解决一个承诺不是返回一个值。要使用 resovled 值,您应该then
像这样使用:
countMD5Hash(data).then(result => {
// do whatever you want with the result
})
但是,如果你countMD5Hash
在async
函数中使用,你也可以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
}
推荐阅读
- powershell - Powershell 如果 -eq 和 -ieq 不区分大小写并且具有相同的目的,或者 -ieq 需要在特定场景中使用?
- java - 为什么在 Spring Boot Project 中直接使用 EntityManger 时出现 NullPointerException?
- angular - Angular:在Routerlink中查找兄弟组件的完整URL字符串
- mysql - 使用 PostgreSQL 和 isGenerated 时,TypeORM 迁移语法错误
- nearprotocol - 在智能合约中使用 env::log 的最佳实践是什么?
- firebase - 热重载使我的应用程序注销 Flutter Firebase
- swift - SwiftUI 颜色不会在主页上更新
- uwp - Windows 10 UWP - 在不同机器上使用 BluetoothLE SDK 示例连接到 BLE 设备时出现问题
- javascript - 使用带有把手的 nodemailer 处理动态电子邮件
- angular - 使用 Angular 获取 KeycloakService 令牌