Ask any question about JavaScript here... and get an instant response.
How can I handle errors from a rejected promise when using async/await in a JavaScript function?
Asked on Dec 12, 2025
Answer
To handle errors from a rejected promise in an async function, use a try-catch block. This allows you to catch and handle any errors that occur during the execution of the awaited promises.
<!-- BEGIN COPY / PASTE -->
async function fetchData() {
try {
const response = await fetch('https://api.example.com/data');
const data = await response.json();
console.log(data);
} catch (error) {
console.error('Error fetching data:', error);
}
}
fetchData();
<!-- END COPY / PASTE -->Additional Comment:
✅ Answered with JavaScript best practices.- Use "try" to wrap the code that might throw an error.
- Use "catch" to handle any errors that occur in the "try" block.
- The "catch" block receives the error object, which you can log or handle as needed.
- This pattern ensures that your code remains clean and errors are managed gracefully.
Recommended Links:
