“asynchron und wartet” Code-Antworten

Async erwartet

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

f();
Yawning Yak

asynchron und wartet

// In JavaScript, An async function is a function declared with the async keyword, and the await keyword can then be permitted within it.
// Example;
function resolveAfter2Seconds() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('resolved');
    }, 2000);
  });
}

async function asyncCall() {
  console.log('calling');
  const result = await resolveAfter2Seconds();
  console.log(result);
  // expected output: "resolved"
}

asyncCall();
// The asnycCall function when run will wait for the await block which calls  resolveAfter2Seconds() before it logs the output to the console.
// We can also use the then method to wait for an async call before running another block of code.
// Example;
async function resolveAfter2Seconds() {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve('resolved');
    }, 2000);
  });
}

resolveAfter2Seconds().then(
  function(result){console.log(result)}  	
  function(error){console.log(result)}  	
);

// There are so many other ways of writing asychronous functions in javascript.
// Javascript is itself an asychronous language.
Chukwujiobi Canon

Ähnliche Antworten wie “asynchron und wartet”

Fragen ähnlich wie “asynchron und wartet”

Weitere verwandte Antworten zu “asynchron und wartet” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen