“асинхронно и ждет” Ответ

Асинхрон ждет

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

асинхронно и ждет

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

Ответы похожие на “асинхронно и ждет”

Вопросы похожие на “асинхронно и ждет”

Больше похожих ответов на “асинхронно и ждет” по JavaScript

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

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