I am using async/await
in my Node.js project. And in some places I need to return an error from async
function. If I'd use Promises, I could've accomplish it this way:
function promiseFunc() {
return new Promise((res, rej) => {
return rej(new Error('some error'))
})
}
But I'm using async
function, so no res
and rej
methods are there.
So, the question: can I throw
errors in async
functions? Or is it considered a good/bad practice?
An example of what I want to do:
async function asyncFunc() {
throw new Error('some another error')
}
I can also rewrite it this way:
async function anotherAsyncFunc() {
return Promise.reject(new Error('we need more errors!'))
}
but the first one looks more clear to me, and I'm not sure which one should I use.