How to reject in async/await syntax?

后端 未结 7 1782
无人及你
无人及你 2020-11-28 17:56

How can I reject a promise that returned by an async/await function?

e.g. Originally:

         


        
7条回答
  •  心在旅途
    2020-11-28 18:25

    This is not an answer over @T.J. Crowder's one. Just an comment responding to the comment "And actually, if the exception is going to be converted to a rejection, I'm not sure whether I am actually bothered if it's an Error. My reasons for throwing only Error probably don't apply."

    if your code is using async/await, then it is still a good practice to reject with an Error instead of 400:

    try {
      await foo('a');
    }
    catch (e) {
      // you would still want `e` to be an `Error` instead of `400`
    }
    

提交回复
热议问题