gpt4 book ai didi

javascript - 使用 fetch.catch 时控制台出现错误

转载 作者:行者123 更新时间:2023-12-03 01:13:54 28 4
gpt4 key购买 nike

我有以下代码:

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() block 正在运行,为什么它会说未捕获(在 promise 中)

最佳答案

你调用了reject,所以这个指向的promise将会被拒绝,如果你没有捕获它,那么它就不会被捕获。要解决该问题,请参阅:

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

关于javascript - 使用 fetch.catch 时控制台出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52104547/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com