首页 > 解决方案 > 有没有办法从外部范围调用异步函数?

问题描述

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 获取值外部范围?

标签: javascriptasync-awaites6-promise

解决方案


推荐阅读