使用fetch.catch时在控制台中出现错误

时间:2018-08-30 20:02:06

标签: javascript fetch-api

我有以下代码:

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 个答案:

答案 0 :(得分:1)

您致电reject,因此指向此承诺的承诺将被拒绝,如果您不注意,那将是未得到的。解决该问题,请参见:

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

相关问题