1

I am using await to make the code cleaner, but I am not sure whether I am handling exceptions correctly.

An example while using 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.

1
  • you need to wrap it into a try catch block Commented Mar 8, 2021 at 17:50

1 Answer 1

4

async functions always return a promise, so you don't need to explicitly create one yourself. Any non-promise value returned from an async function is implicitly wrapped in a 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.

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

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

    If the promise returned by coreApiObject.getTeams(...) is rejected, promise returned by the 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.

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

    Other option is to throw the error from the catch block to make sure that the promise returned by the async function is rejected.

    If you don't throw the error or return a promise or a thenable that is rejected, returning any other value from the catch block will fulfil the promise returned by the async function with whatever value is returned inside the catch block.

Sign up to request clarification or add additional context in comments.

3 Comments

You are saying if I sing a function as async and return a value, the value automatically will be wrapped in a promise object. Right?
In this case, what if I also want the caller function to handle the exception
mozilla.org says, "A Promise which will be resolved with the value returned by the async function, or rejected with an exception thrown from, or uncaught within, the async function."

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.