how to catch error in async await javascript code example
Example 1: async await catch error
async function f() {
try {
let response = await fetch('/no-user-here');
let user = await response.json();
} catch(err) {
// catches errors both in fetch and response.json
alert(err);
}
}
Example 2: catch errors async await javascript
try {
await fetchUserData(userId)
} catch (e) {
console.log('asynchronous error was caught!');
handleError(e);
}