“Javascript Async” Respostas de código

Awiat assíncrono

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

data();
Salo Hopeless

Defina uma função assíncrona

const yourAsyncFunction = async () => {
    // do something asynchronously and return a promise
    return result;
  }
  
anArray.forEach(async item => {
   // do something asynchronously for each item in 'anArray'
   // one could also use .map here to return an array of promises to use with 'Promise.all()'
 });
 
server.getPeople().then(async people => {
  people.forEach(person => {
    // do something asynchronously for each person
  });
});
Xifré Font

função assíncrona em javascript

const foo = async () => {
   await// do something
}
// OR
async function foo() {
  await// do something
}
Inquisitive Iguana

O que é uma função assíncrona

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

assíncrono aguardando

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

Javascript Async

function myFunction() {
  return Promise.resolve("Hello");
}
naly moslih

Respostas semelhantes a “Javascript Async”

Procure respostas de código populares por idioma

Procurar outros idiomas de código