javascript - 有没有办法从外部范围调用异步函数?
问题描述
getPhotoStatus();
async function getPhotoStatus () {
let res = await fetchPhoto('/photos/19062657811.png');
console.log(res);
}
async function fetchPhoto(uri) {
const status = fetch(uri).then(response => response.status);
return status;
}
我在这里有这段代码,它工作正常,但删除 getPhotoStatus() 并直接在 fetchPhoto() 中等待 fetch 函数返回一个 Promise,有没有另一种方法可以做到这一点,而不是基本上使用双重函数来直接从 Promise 获取值外部范围?
解决方案
推荐阅读
- python - 如何使按钮出现在背景后面,或使按钮背景透明?
- raster - 在 OpenLayers 5 中对 OSM 源使用光栅操作
- mysql - 查找其他两列mysql中对的第三列的总和
- pandas - 如何使用熊猫查找列的最大值并按顺序排列它们?
- sql - 如何使用 SQL Server 中包含“&”的全文索引查询行?
- ios - UIView.animation 没有动画颜色变化
- angular - 如何使用角度更改鼠标输入的文本颜色
- linear-regression - 线性回归和特征缩放
- php - 卷文件更改不会在浏览器中传播
- javascript - 如何将具有许多状态变量的循环嵌套到表中?