1

我粘贴了两个几乎相同的代码,但略有不同,一个工作正常,但另一个给出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 中解释异步函数的这种行为吗?

4

1 回答 1

0

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

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

关于执行者,了解以下内容很重要:

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

async函数将其returnthrow调用转换为 Promise 解析,但async函数始终返回 Promise。所以在你的第二个例子中,你的throw结果是一个被拒绝的 Promise,然后是 unhandled;如上所述,返回值被完全丢弃。如果不是因为 UnhandledPromiseRejectionWarning,您throw new Error()将没有任何症状,并且您promise()返回的承诺永远不会解决。

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

于 2022-02-01T18:33:14.757 回答