“asíncrito y espera” Código de respuesta

asíncea espera

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

asíncrito y espera

// 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

Respuestas similares a “asíncrito y espera”

Preguntas similares a “asíncrito y espera”

Más respuestas relacionadas con “asíncrito y espera” en JavaScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código