“Javscript async/espera” Código de respuesta

¿Qué es una función async?

// Old School Javascript Invoke
(async function() {
	await someAsyncFunction();
})();

//New ES6 Javascript Invoke
(async () => {
	await someAsyncFunction();
})();

//Example (async & await)
function delayResult() {
 return new Promise(resolve => {
   setTimeout(() => {
     resolve(‘Done’);
   }, 5000)
 })
}
async function getResult() {
 let result = await delayResult();
 return result;
}
getResult();

//New Example 
const data = async ()  => {
  const got = await fetch('https://jsonplaceholder.typicode.com/todos/1');
  
  console.log(await got.json())
}

data();
Tough Tuatara

Async espera js

fetch('coffee.jpg')
.then(response => {
  if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`);
  } else {
    return response.blob();
  }
})
.then(myBlob => {
  let objectURL = URL.createObjectURL(myBlob);
  let image = document.createElement('img');
  image.src = objectURL;
  document.body.appendChild(image);
})
.catch(e => {
  console.log('There has been a problem with your fetch operation: ' + e.message);
});
Tired Toucan

Respuestas similares a “Javscript async/espera”

Preguntas similares a “Javscript async/espera”

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código