Answers for "async await and try catch for exception handling"

21

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);
  }
}
Posted by: Guest on March-21-2020
1

try catch async await

async function getData() {
    try {
        const response = await fetch('https://jsonplaceholder.typicode.com/todos/1')
        const data = await response.json();
      	const { userId, id, title, completed } = data;
      	console.log(userId, id, title, completed);
    } catch(err) {
      alert(err)
    }
}
getData();
Posted by: Guest on January-04-2022

Code answers related to "async await and try catch for exception handling"

Code answers related to "Javascript"

Browse Popular Code Answers by Language