简体   繁体   English

什么是 UnhandledPromiseRejectionWarning:错误?

[英]What is UnhandledPromiseRejectionWarning: Error?

I've pasted two almost same codes with one little difference, one works just fine but the other gives UnhandledPromiseRejectionWarning .我粘贴了两个几乎相同的代码,但略有不同,一个工作正常,但另一个给出UnhandledPromiseRejectionWarning

async function promise () {
  return new Promise((resolve, reject) => {
    throw new Error();
    resolve();
    reject();
  })
}

promise().then((data) =>{console.log('then')}).catch((err)=>{console.log('from the catch')});

The output is: output 是:

> node index.js
from the catch

But for this case但是对于这种情况

function promise () {
  return new Promise(async (resolve, reject) => {
    throw new Error();
    resolve();
    reject();
  })
}

promise().then((data) =>{console.log('then')}).catch((err)=>{console.log('err')});

The output is something like this output 是这样的

> node index.js
(node:98195) UnhandledPromiseRejectionWarning: Error
    at /home/parthiv/Projects/exp/index.js:47:11
    at new Promise (<anonymous>)
    at promise (/home/parthiv/Projects/exp/index.js:46:10)
    at Object.<anonymous> (/home/parthiv/Projects/exp/index.js:53:1)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47
(Use `node --trace-warnings ...` to show where the warning was created)
(node:98195) UnhandledPromiseRejectionWarning: Unhandled promise rejection. 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(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:98195) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Can someone explain this behaviour of an async function inside the promise?有人可以解释 promise 内的异步 function 的这种行为吗?

You've passed an async function into a Promise constructor, which is not the right thing to do.您已将async function 传递给 Promise 构造函数,这不是正确的做法。 The return value of the async function is what is unhandled. async function 的返回值是未处理的。

The Promise constructor is designed for synchronous usage, and it completely disregards its return value. Promise 构造函数是为同步使用而设计的,它完全忽略了它的返回值。 If you throw in the function you pass to a Promise constructor, the Promise constructor will return a rejected promise;如果您throw传递给 Promise 构造函数,则 Promise 构造函数将返回被拒绝的 ZB321DE3795DC29D9DEEBZE; that's the behavior you see in your first example.这就是您在第一个示例中看到的行为。 If the function returns a value, even a Promise, it won't matter: the value is discarded .如果 function 返回一个值,即使是 Promise,也没关系: 该值被丢弃

About the executor, it's important to understand the following:关于执行者,了解以下内容很重要:

  • The executor return value is ignored.执行器返回值被忽略。
  • If an error is thrown in the executor, the promise is rejected.如果在执行器中抛出错误,则 promise 将被拒绝。

async functions convert its return and throw calls to Promise resolution, but async functions always return a Promise. async函数将其returnthrow调用转换为 Promise 分辨率,但async函数总是返回 Promise。 So in your second example, your throw results in a rejected Promise, which is then unhandled ;因此,在您的第二个示例中,您的throw导致被拒绝的 Promise ,然后未处理 the return value is discarded entirely as above.如上所述,返回值被完全丢弃。 If it weren't for the UnhandledPromiseRejectionWarning, your throw new Error() would have no symptoms, and the promise your promise() returned would never resolve.如果不是 UnhandledPromiseRejectionWarning,您的throw new Error()将没有任何症状,并且您的promise()返回的 promise 将永远无法解决。

If you want promise() to return a promise, you can just return the return value of the inner async function, or (even better) make promise() itself an async function.如果你想让promise()返回一个 promise,你可以只返回内部async function 的返回值,或者(甚至更好)使promise()本身成为一个异步 ZC1C425268E68385D145074C17A9。 If you do need the resolve / reject calls, you can always return new Promise() from inside the function;如果您确实需要resolve / reject呼叫,您可以随时从 function 内部return new Promise() whenever an async function or .then() handler returns a Promise, that return value is unwrapped.每当async function 或 .then .then()处理程序返回 Promise 时,该返回值被解包。

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

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