I always use async/await and .catch() instead of using async/await and try/catch to make code compactly.
async function asyncTask() {
throw new Error('network')
}
async function main() {
const result = await asyncTask().catch(error => console.error(error));
console.log('result:', result)
}
main();
If you want to get a fallback value when an error happened, you can ignore the error and return a value inside the .catch() method
async function asyncTask() {
throw new Error('network')
}
async function main() {
const result = await asyncTask().catch(_ => 'fallback value');
console.log('result:', result)
}
main();