被捕获(诺言)...使用捕获

发布于 2025-01-30 06:58:56 字数 609 浏览 2 评论 0原文

为什么以下代码报告被捕获的错误时,被拒绝的错误?

function Test() {
  this.start = function(action) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        if (action == "fail") return reject("rejected");
        resolve("resolved");
      }, 1000);
    });
  }
}

const test = new Test();
const promise = test.start("fail");
promise.then(console.log);
promise.catch(console.error);

输出(在浏览器中)为:

catch()

uck> und offerf(承诺)被拒绝的从呼叫拒绝的

Why does the following code report an Uncaught (in promise) rejected error when it is being caught?

function Test() {
  this.start = function(action) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        if (action == "fail") return reject("rejected");
        resolve("resolved");
      }, 1000);
    });
  }
}

const test = new Test();
const promise = test.start("fail");
promise.then(console.log);
promise.catch(console.error);

The output (in a browser) is:

rejected from the catch()

Uncaught (in promise) rejected from the call to reject

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

童话里做英雄 2025-02-06 06:58:56

分叉您的承诺链:

promise ---> .then( .. ) ---> ???
        \
         \
          +-> .catch( .. )

拒绝是被.catch抓住的,但是它也可以通过。然后分支。这两个分支都独立解决。而且,您不会在上捕获拒绝。然后,然后导致拒绝错误。

比较:

promise.then(console.log).catch(console.error)

这里的链条看起来像这样:

promise ---> .then( .. ) ---> .catch( .. )

拒绝将 skip 。然后处理程序,然后转到最近的.catch

You're forking your promise chain:

promise ---> .then( .. ) ---> ???
        \
         \
          +-> .catch( .. )

The rejection is being caught by the .catch just fine, but it also goes through the .then branch. Both of those branches are resolved independently. And you're not catching the rejection on the .then branch, leading to the uncaught rejection error.

Compare with:

promise.then(console.log).catch(console.error)

Here the chain looks like this:

promise ---> .then( .. ) ---> .catch( .. )

The rejection will skip the .then handler and go to the nearest .catch.

七月上 2025-02-06 06:58:56

好的。答案很明显,但很容易错过。

。然后(...)返回另一个承诺。因此,您必须将.catch(...)附加到其中。

解决方案:

const promise = test.start("fail");
promise.then(console.log).catch(console.error);  

或更明确:

const promise = test.start("fail");
const promise2 = promise.then(console.log);
promise2.catch(console.error);

@deceze的答案/解释非常好。

Ok. The answer is obvious, yet easy to miss.

.then(...) returns another promise. So you have to attach the .catch(...) to THAT.

Solution:

const promise = test.start("fail");
promise.then(console.log).catch(console.error);  

OR more explicitly:

const promise = test.start("fail");
const promise2 = promise.then(console.log);
promise2.catch(console.error);

@deceze's answer/explaination is pretty good.

蓝梦月影 2025-02-06 06:58:56

这是校正的代码:

function Test() {
  this.start = function(action) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        if (action == "fail") return reject(new Error("rejected"));
        resolve("resolved");
      }, 1000);
    });
  }
}

const test = new Test();
const promise = test.start("fail");
promise.then(console.log).catch(console.error);

您需要链链

Here is the corrected code :

function Test() {
  this.start = function(action) {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        if (action == "fail") return reject(new Error("rejected"));
        resolve("resolved");
      }, 1000);
    });
  }
}

const test = new Test();
const promise = test.start("fail");
promise.then(console.log).catch(console.error);

you need to chain .then() and .catch() for it to work,

because .then() will return a new promise, and it's that promise that will throw the error you need to catch

网名女生简单气质 2025-02-06 06:58:56

没有任何承诺

因为您

promise.then(console.log).catch(console.error);


将您的拒绝转化为解决您的承诺,

function Test() {
    this.start = function(action) {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                if (action == "fail") return resolve("rejected");
                return resolve("resolved");
            }, 1000);
        });
    }
}

希望这会有所帮助

Because there's no promise made for your .then

Either you change your function call into

promise.then(console.log).catch(console.error);

or
change your reject into resolve on your promise

function Test() {
    this.start = function(action) {
        return new Promise((resolve, reject) => {
            setTimeout(() => {
                if (action == "fail") return resolve("rejected");
                return resolve("resolved");
            }, 1000);
        });
    }
}

hope this helps

心作怪 2025-02-06 06:58:56

您的代码说以下内容:

const test = new Test();
const promise = test.start("fail");

您创建承诺并给出字符串参数(“失败”)。

promise.then(console.log);
promise.catch(console.error);

您会抓住承诺,并显示

if (action == "fail") return reject("rejected");

出带有字符串参数“拒绝”的拒绝函数的错误。

希望它有帮助。

Your code says the following:

const test = new Test();
const promise = test.start("fail");

You create a promise and give a string parameter ("fail").

promise.then(console.log);
promise.catch(console.error);

You catch the promise and show the error

if (action == "fail") return reject("rejected");

You return the reject function with the string parameter "rejected".

Hope it help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文