【发布时间】:2021-05-27 21:07:38
【问题描述】:
我正在尝试使用 fetch API,我试图返回一个日志获取错误,我所做的是捕获错误并记录捕获捕获的错误,它运行良好,但是当我在上添加更多 console.log catch 块无论 fetch 是否发现错误,它都会执行。为什么会这样?以及如何在 catch 块上返回错误以外的数据?我的代码将对其进行更多解释。提前致谢。
fetch('api', options)
.then(response => response.json())
.then(data => console.log(data)
})
.catch(err =>
console.log('error', err), //prints error on error
console.log("this is test") //print regardless of error(prints everytime the program runs even without error)
);
【问题讨论】:
-
err => 1, f()是(err => 1), f(),您正在调用.catch,并将console.log调用作为第二个参数,其中包括调用console.log。
标签: javascript node.js