For error handling in async/await in JavaScript, normally I see people use try catch to handle this.
e.g.
async function apiCall() {
throw new Error('failed')
}
async function run() {
try {
const response = await apiCall()
if(response.status === 'success') {
console.log('success!')
}
} catch(e) {
console.log('e', e)
}
}
I never really liked try-catch block then I recently happened to know there is another way to handle it without relying on try-catch block
async function run2() {
const response = await apiCall().catch(console.log)
if(!response) return
if(response.status === 'success') {
console.log('success!')
}
}
But the downside is that it will continue executing the following lines even if there is an error so we have to manually return in the case of an error.
I wonder which way is better or in which cases one is preferred over the other?