简体   繁体   English

使用等待时使用 catch 处理 Promise 拒绝

[英]Handling Promise rejection with catch while using await

I am using await to make the code cleaner, but I am not sure whether I am handling exceptions correctly.我正在使用await使代码更干净,但我不确定我是否正确处理了异常。

An example while using azure-devops-node-api;使用 azure-devops-node-api 的示例;

const foo = async() => {
    return new Promise((resolve, reject) => {
        ...
        ...
        const teams = await coreApiObject.getTeams(currProject.id)
                      .catch(err => { reject(err)  return })
        ...
        ...
    })  
}

In this code I am assuming, if there is a problem with promise call, foo() is going to return reject.在这段代码中,我假设,如果 promise 调用有问题,foo() 将返回拒绝。

async functions always return a promise, so you don't need to explicitly create one yourself. async函数总是返回 promise,因此您不需要自己显式创建一个。 Any non-promise value returned from an async function is implicitly wrapped in a promise.从异步 function 返回的任何非承诺值都隐式包装在 promise 中。

Inside the foo function, you just need to await the call coreApiObject.getTeams(...) and to catch and handle any error, use the try-catch block.foo function 内部,您只需要await调用coreApiObject.getTeams(...)并捕获和处理任何错误,使用try-catch块。

Your code can be simplified as shown below:您的代码可以简化如下所示:

const foo = async() => {
   try {
      const teams = await coreApiObject.getTeams(currProject.id);
      return teams;
   } catch (e) {
      // handle error
   } 
}

If you want to the calling code to handle the error, then you can use one of the following options:如果您想调用代码来处理错误,那么您可以使用以下选项之一:

  • Remove the try-catch block and just return the result of coreApiObject.getTeams(...) .删除try-catch块并返回coreApiObject.getTeams(...)的结果。

     const foo = async() => { return coreApiObject.getTeams(currProject.id); }

    Removing the try-catch block and just returning the call to coreApiObject.getTeams(...) will allow the calling code to handle the error because the promise returned by the foo function will get resolved to the promise returned by coreApiObject.getTeams(...) : this means that the fate of the promise returned by the foo function will depend on whatever happens to the promise returned by coreApiObject.getTeams(...) . Removing the try-catch block and just returning the call to coreApiObject.getTeams(...) will allow the calling code to handle the error because the promise returned by the foo function will get resolved to the promise returned by coreApiObject.getTeams(...) foo coreApiObject.getTeams(...)

    If the promise returned by coreApiObject.getTeams(...) is rejected, promise returned by foo function will also be rejected and hence the calling code will have a change to catch the promise rejection and handle it. If the promise returned by coreApiObject.getTeams(...) is rejected, promise returned by foo function will also be rejected and hence the calling code will have a change to catch the promise rejection and handle it.

  • Throw the error from the catch block.catch块中抛出错误。

     const foo = async() => { try { const teams = await coreApiObject.getTeams(currProject.id); return teams; } catch (error) { // throw the error throw error; } }

    You need to throw the error from the catch block to make sure that the promise returned by the async function is rejected ;您需要从catch块中抛出错误,以确保async function 返回的 promise 被拒绝 if you return a value from the catch block, promise returned by the async function will resolve with whatever value is returned by the catch block.如果从catch块返回一个值,则async function 返回的 promise 将使用catch块返回的任何值解析

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

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