await and catch 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: await and catch javascript
var response = await promisedFunction().catch((err) => { console.error(err); });
// response will be undefined if the promise is rejected
// this will work instead of using try...catch...