“JavaScript Async” Kode Jawaban

Async Awiat

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

data();
Salo Hopeless

Tentukan fungsi async

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

fungsi async dalam javascript

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

Apa itu fungsi 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 menunggu

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

Jawaban yang mirip dengan “JavaScript Async”

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya