【问题标题】:What is UnhandledPromiseRejectionWarning: Error?什么是 UnhandledPromiseRejectionWarning:错误?
【发布时间】:2022-02-02 02:33:14
【问题描述】:

我粘贴了两个几乎相同的代码,但有一点不同,一个工作正常,但另一个给出 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')});

输出是:

> node index.js
from the catch

但是对于这种情况

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

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

输出是这样的

> 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.

有人能解释一下 promise 中异步函数的这种行为吗?

【问题讨论】:

标签: javascript asynchronous promise unhandled-promise-rejection


【解决方案1】:

您已将 async 函数传递给 Promise 构造函数,这是不正确的做法。 async 函数的返回值是未处理的。

Promise 构造函数是为同步使用而设计的,它完全忽略了它的返回值。如果你在传递给 Promise 构造函数的函数中 throw,Promise 构造函数将返回一个被拒绝的 Promise;这就是您在第一个示例中看到的行为。如果函数返回一个值,即使是 Promise,也没关系:the value is discarded

关于执行者,了解以下几点很重要:

  • 忽略执行器返回值。
  • 如果在 executor 中抛出错误,则 promise 被拒绝。

async 函数将其 returnthrow 调用转换为 Promise 解析,但 async 函数总是返回一个 Promise。因此,在您的第二个示例中,您的 throw 导致被拒绝的 Promise,然后未处理;如上所述,返回值被完全丢弃。如果没有 UnhandledPromiseRejectionWarning,您的 throw new Error() 将没有任何症状,您的 promise() 返回的承诺将永远无法解决。

如果你想让promise() 返回一个promise,你可以只返回内部async 函数的返回值,或者(甚至更好)使promise() 本身成为一个异步函数。如果您确实需要resolve/reject 调用,您可以随时从函数内部return new Promise();每当 async 函数或 .then() 处理程序返回一个 Promise 时,该返回值就会被解包。

【讨论】:

    猜你喜欢
    • 2018-02-23
    • 2021-11-17
    • 1970-01-01
    • 2019-11-02
    • 1970-01-01
    • 2019-11-24
    • 2021-10-04
    • 2021-07-01
    • 1970-01-01
    相关资源
    最近更新 更多