javascript - `await image.onload;` 是如何工作的?onload 为空,但等待等待 onload
问题描述
我正在寻找一种获取图像大小的方法。
我了解到 Promise 和 async/await 可以在 image.onload 中使用。
然而,与许多示例不同的是,我发现它只需在代码中等待空状态 onload 即可工作。
为什么这很好用?
这是我的代码
const base64 = reader.result;
reader.onloadend = async () => {
if (base64) {
let img = new Image();
img.src = base64.toString();
await img.onload; // <-- null, Here's my question
// img succesfully loaded.
});
...
}
添加
正常使用方法:
const base64 = reader.result;
if (base64) {
let img = new Image();
img.src = base64.toString();
img.onload = async () => {
// setState or Do something.
};
但是我没有指定任何回调方法,而是等待图像加载完毕。
我想知道为什么当 onload 为空时你在等我。
解决方案
推荐阅读
- python - 调用类中的对象时不可调用 Int 对象
- python - 如何在 python3 中键入提示 matplotlib.axes._subplots.AxesSubplots 对象
- node.js - 使用 Node.JS 编辑 XML 文件
- python - 在不填充的情况下调整 numpy 数组的大小
- html - Keycloak 领域登录页面渲染问题
- r - 使用重叠包将时间转换为太阳时间时出错
- eclipse - AEM WKND 教程组件基础 eclipse 无法识别 jcr 属性
- c++11 - C++11调用虚成员函数
- javascript - Hotw 将片段的动态数据加载到 thymeleaf 项目中的另一个 HTML 页面
- python - SNMP pass_persist 似乎没有执行