“Async warten auf den Fangfehler” Code-Antworten

Async warten auf den Fangfehler

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);
  }
}
Victor Grk

Versuchen Sie es zu erwarten

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();
Anthony Smith

Async erwartet .Catch

function getQuote() {
  let quote = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
  return quote;
}

async function main() {
  try {
    var quote = await getQuote();
    console.log(quote);
  } catch (error) {
    console.error(error);
  }
}

main();
Runtime Terror

Ähnliche Antworten wie “Async warten auf den Fangfehler”

Fragen ähnlich wie “Async warten auf den Fangfehler”

Weitere verwandte Antworten zu “Async warten auf den Fangfehler” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen