简体   繁体   English

没有“await”关键字的异步 function 返回“未捕获(承诺中)错误”

[英]Async function without “await” keyword returns the “Uncaught (in promise) error”

I am fairly new to asynchronous functions and had experimented with the following scenarios:我对异步函数相当陌生,并尝试过以下场景:

Scenario A方案 A

async function foo() {
   const p2 = new Promise((_,reject) =>  reject('2')); //Uncaught (in promise) 2
}
foo().catch(() => {})

Scenario B方案 B

async function foo() {}
foo().catch(() => {})

My question is why does the error message occur in Scenario A?我的问题是为什么在场景 A 中会出现错误消息? Both scenarios implicitly return a resolved promise with the value of "undefined," but only the error message shows on Scenario A. I also read that a promise without.catch() specified will hide its error message, but it clearly does not do so in the above example.这两种情况都隐式返回一个解析的 promise,其值为“未定义”,但仅在场景 A 上显示错误消息。我还读到没有指定的 promise 将隐藏其错误消息,但显然没有这样做在上面的例子中。 I have been stuck for over an hour and any help is greatly appreciated!我已经被困了一个多小时,非常感谢任何帮助!

You aren't returning your p2 promise from your function, therefore, there's no way for the caller to catch() the result of that promise.您没有从 function 返回您的p2 promise,因此,调用者无法catch()该 promise 的结果。 And, in this particular example, there is no reason for your function to be async since you aren't using await and you aren't throwing and expecting that to be turned into a rejected promise.而且,在这个特定的示例中,没有理由让您的 function async ,因为您没有使用await并且您没有抛出并期望将其变成被拒绝的 promise。 So, your uncaught promise doesn't have anything to do with async here.因此,您未捕获的 promise 与此处的async没有任何关系。

async function foo() {
   const p2 = new Promise((_,reject) =>  reject('2'));
   return p2;
}

foo().catch(() => {})

or remove the unnecessary async keyword:或删除不必要的async关键字:

function foo() {
   const p2 = new Promise((_,reject) =>  reject('2'));
   return p2;
}

foo().catch(() => {})

My question is why does the error message occur in Scenario A?我的问题是为什么在场景 A 中会出现错误消息? Both scenarios implicitly return a resolved promise with the value of "undefined," but only the error message shows on Scenario A.两种方案都隐式返回一个解析的 promise,其值为“未定义”,但仅在方案 A 上显示错误消息。

Because you're creating a promise p2 and then doing nothing with it.因为您正在创建 promise p2 ,然后什么也不做。 It isn't connected at all to the return value of foo() or to the promise that is automatically returned from the async function foo .它根本不连接到foo()的返回值或从async function foo自动返回的 promise 。 It's just orphaned there by itself and thus nobody catches its error.它本身就是孤儿,因此没有人发现它的错误。

I also read that a promise without.catch() specified will hide its error message, but it clearly does not do so in the above example我还读到没有指定.catch() 的 promise 将隐藏其错误消息,但在上面的示例中显然没有这样做

You'd have to show us the context in which you read that because by itself that is not correct.您必须向我们展示您阅读该内容的上下文,因为这本身是不正确的。 A rejected promise where that rejection is never caught will cause a warning.被拒绝的 promise 永远不会被拒绝将导致警告。


If you await your p2 promise, then the await is tied to the promise that the async function returns and that will work properly.如果您await您的p2 promise,那么awaitasync function 返回的 promise 相关联,它将正常工作。 So, this will work:因此,这将起作用:

async function foo() {
   const p2 = new Promise((_,reject) =>  reject('2'));
   await p2;
}

foo().catch(() => {})

The await will see the rejection. await将看到拒绝。 That will then cause the promise that the async function returned to reject and the caller will see that rejection and your .catch() will catch it.这将导致async function 返回的 promise 被拒绝,调用者将看到该拒绝,并且您的.catch()将捕获它。 Without the await , this is just an orphaned promise.没有await ,这只是一个孤立的 promise。 async functions don't connect to all rejections inside them - they only connect to rejections that are awaited or are directly returned. async函数不会连接到它们内部的所有拒绝 - 它们只连接到等待或直接返回的拒绝。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM