“JavaScript async” Ответ

Асинхровый Ауат

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

data();
Salo Hopeless

Определите асинхронную функцию

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

асинхронная функция в JavaScript

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

Что такое асинхронная функция

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

Ответы похожие на “JavaScript async”

Смотреть популярные ответы по языку

Смотреть другие языки программирования