代码之家  ›  专栏  ›  技术社区  ›  Abdul Ahmad

使用时在控制台中获取错误获取.捕获

  •  0
  • Abdul Ahmad  · 技术社区  · 7 年前

    我有以下代码:

    fetch(
      url,
      { ...data }
    ).then((response) => {
      if (!response.ok) throw new Error(response.statusText);
      return response.json();
    }).then((response) => {
      resolve(response);
    }).catch((error) => {
      console.log('error', error);
      reject(error);
    });
    

    404 ,的 console.log('error') 行运行,但控制台中仍出现错误:

    GET https://swapi.co/api/people/0/ 404 ()
    
    Uncaught (in promise) Error
        at http.js:10
    

    catch() 布洛克跑了,为什么说 uncaught (in promise) ?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Jonas Wilms    7 年前

    你打电话来 reject ,所以这个指向的承诺将被拒绝,如果你不明白这是不可能的。要解决此问题,请参见:

    What is the explicit promise construction antipattern and how do I avoid it?