JavaScript Async/Await

In JavaScript Async/Await introduced in ES8 is great since it makes our code cleaner compared to promise chains. Let's take a look at a real world example of using async/await in calling an API.

  • We can declare a function async by adding the keyword. This will return a promise; each async function will contain an await expression which makes js wait until that promise returns the result.
  • Below is a good real world example & checkout my codepen if you want to try it
  • 1const getDogData = async () => {
    2    const responsePageData = await fetch('https://dog.ceo/api/breeds/image/random');
    3    const dogData = await responsePageData.json();
    4    console.log("Our dog data will be here! ", dogData);
    5};
    6getDogData();