首页 > 解决方案 > fetch 是否只抛出 TypeError

问题描述

我可以假设 fetch 只能抛出TypeError 的错误吗?

const request = async () => {
    let response = null;
    try {
     response = await fetch('https://api.com/values/1');
    } catch (err){
      //is the err here is always of type typeError
    }
    if(!response.ok){
      //bla bla bla 401/403 ...
    }
    const json = await response.json();
    console.log(json);
}

request();

标签: javascriptreactjsfetch

解决方案


也可以fetch抛出一个AbortError. 有关更多详细信息,请参阅文档


推荐阅读