Skip to content
Shubham JangleResources
JavaScript

Explore topics

What is async-await

Watch the walkthrough
Open on Instagram

Revision clarifications

An async function always returns a promise. await suspends that function, not the browser thread. Independent requests can be started together and awaited with Promise.all instead of sequential awaits; use sequential awaits when one result is needed by the next step.

The source notes below are copied unchanged apart from line endings. Examples are independent; some deliberately throw or depend on browser markup.

Original HackTheCode notes

Read the original Markdown

Javascript introduced async/await in ES8 (ECMAScript 2017) to write more readable async code which feels more like synchronous code. Before that we were using callbacks which we saw in last videos then we shifted to promises which was introduced in ES6 (ECMAScript 2015).

1. using promises

const fetchProjectDetails = () =>
  fetch("https://jsonplaceholder.typicode.com/posts/1").then((res) =>
    res.json()
  );

const fetchTeamMembers = () =>
  fetch("https://jsonplaceholder.typicode.com/users/1").then((res) =>
    res.json()
  );

const fetchRecentActivities = () =>
  fetch("https://jsonplaceholder.typicode.com/comments/1").then((res) =>
    res.json()
  );

fetchProjectDetails()
  .then((project) => {
    console.log("Project Details:", project.title);
    return fetchTeamMembers();
  })
  .then((teamMembers) => {
    console.log("Team Members:", teamMembers.name);
    return fetchRecentActivities();
  })
  .then((activities) => {
    console.log("Recent Activities:", activities.name);
  })
  .catch((error) => {
    console.error("Error fetching data:", error);
  });

2. Using async await

const fetchProjectDetails = () =>
  fetch("https://jsonplaceholder.typicode.com/posts/1").then((res) =>
    res.json()
  );

const fetchTeamMembers = () =>
  fetch("https://jsonplaceholder.typicode.com/users/1").then((res) =>
    res.json()
  );

const fetchRecentActivities = () =>
  fetch("https://jsonplaceholder.typicode.com/comments/1").then((res) =>
    res.json()
  );

const fetchAllData = async () => {
  try {
    const project = await fetchProjectDetails();
    console.log("Project Details:", project.title);

    const teamMembers = await fetchTeamMembers();
    console.log("Team Members:", teamMembers.name);

    const activities = await fetchRecentActivities();
    console.log("Recent Activities:", activities.name);
  } catch (error) {
    console.error("Error fetching data:", error);
  }
};

fetchAllData();