gpt4 book ai didi

javascript - 集成 .catch after 函数

转载 作者:行者123 更新时间:2023-12-04 08:17:48 26 4
gpt4 key购买 nike

我以前从未使用过 .catch,而且我在网上阅读的文档没有多大意义。
有了这条线:

steve.open(req.params.url).then(function(site) {
我收到此错误:
This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch().
我以前从未使用过 .catch。任何人都可以帮忙吗?
我知道我的 .then({})我应该添加一个 .catch((e) => { do something with error })但是在那条线之后会是什么样子呢?我似乎无法让它工作

最佳答案

您可以将调用链接到 .catch()在您现有的代码之后:

steve.open(req.params.url)
.then(response => {
// Do something...
})
.catch(error => {
// Handle the error...
});
或者您可以在对 .then() 的调用中声明错误回调:
steve.open(req.params.url)
.then(response => {
// Do something...
}, error => {
// Handle the error...
});
有一点不同:将多个调用链接到 .then()将有不同的错误处理行为,具体取决于您是在单个调用中提供错误回调还是调用 .catch()在链的末端。
Promises 是一个复杂的话题,我建议在 documentation 中阅读更多关于它们的信息。 .

关于javascript - 集成 .catch after 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65638596/

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