Skip to content

Latest commit

 

History

History
executable file
·
29 lines (24 loc) · 677 Bytes

async-await.md

File metadata and controls

executable file
·
29 lines (24 loc) · 677 Bytes

Async Await

  • write async code that reads like sync code
  • inside a function marked as async, you can place await in front of a promise
  • the execution of the async function gets paused until the promise is resolved

Promise:

function fetchUrl(id) {
  return fetch(`https://.../${id}`)
    .then((response) => response.json())
    .then((data) => data.name);
}
const result = fetchUrl(123);
console.log(result); // => miku86

Async Await:

async function fetchUrl(id) {
  const response = await fetch(`https://.../${id}`);
  const data = await response.json();
  return data.name;
}
const result = fetchUrl(123);
console.log(result); // => miku86