如何在catch块中使用async/await获取失败的Promise?

huangapple go评论69阅读模式
英文:

How to get the promise that failed in catch block using async/await?

问题

我正在将一些代码从.then/.catch转换为async/await。我需要在catch块中访问失败的原始Promise以进行日志记录。

原始代码:

const myPromise = somePromise()

myPromsise.then(() => {
  //...
}).catch((error) => {
  errorLogger(error, myPromise) // 我们记录日志的方式需要将原始Promise传递给错误记录器。
}

使用try catch:

try {
  const myPromise = await somePromise()
  //...
} catch (error) {
  errorLogger(error, myPromise) // 由于作用域的原因,myPromise在catch块中不可用。
}

如何在catch块中访问Promise?

英文:

I'm converting some code from .then/.catch to async/await. I need to access the original promise that fails in the catch block for logging purposes.

Orignal code:

  const myPromise = somePromise()

  myPromsise.then(() => {
    //...
  }).catch((error) => {
    errorLogger(error, myPromise) // The way we log things requires the original promise to be passed to the error logger.
  }

With try catch

  try {
    const myPromise = await somePromise()
    //...
  } catch (error) {
    errorLogger(error, myPromise) // myPromise is not available in catch block due to scope.
  }

How can I access the promise in the catch block?

答案1

得分: 3

你没有将myPromise()设置为Promise。通过使用await,你将其设置为Promise解析的值。

要获取Promise本身,请将该函数分配给一个变量,而不使用await。然后,你可以单独使用await来获取已解析的值。

const myPromise = somePromise();
try {
    const myValue = await myPromise;
    //...
} catch (error) {
    errorLogger(error, myPromise);
}
英文:

You're not setting myPromise() to the promise. By using await you're setting it to what the promise resolves to.

To get the promise itself, assign the function to a variable without using await. You can then use await separately to get the resolved value.

const myPromise = somePromise();
try {
    const myValue = await myPromise;
    //...
} catch (error) {
    errorLogger(error, myPromise);
}

答案2

得分: 0

以下是翻译好的内容:

"我们记录日志的方式要求原始的 promise 被传递给错误记录器。

为什么?这没有意义,代码在没有可用的 promise 的情况下可能会失败的多种方式。实际上,记录器所需的所有信息应该包含在作为异常抛出的错误对象中。修复您的日志记录。

但如果您一定要这样做,我会从以下方式开始:

try {
  const myPromise = somePromise();
  await myPromise.catch(err => { err.promise = myPromise; throw err; });
  
} catch (error) {
  errorLogger(error, error.promise)
}
英文:

> The way we log things requires the original promise to be passed to the error logger.

Why? This doesn't make sense, there are many ways code can fail without a promise being available. Really all the information that the logger needs should be contained in the error object that is thrown as the exception. Fix your logging.

But if you must, I would start with

try {
  const myPromise = somePromise();
  await myPromise.catch(err => { err.promise = myPromise; throw err; });
  …
} catch (error) {
  errorLogger(error, error.promise)
}

huangapple
  • 本文由 发表于 2023年7月11日 08:34:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定