Synchronous vs asynchronous

๐Ÿ”„ First, what does async/await do?


๐Ÿง  So how is it asynchronous?

๐Ÿ” Async/Await Step-by-Step Example

Let's break it down step by step:

console.log("1. Start");

async function fetchData() {
  console.log("2. Inside async function");

  const response = await fetch('https://jsonplaceholder.typicode.com/todos/1');

  console.log("4. After await");

  const data = await response.json();
  console.log("5. Data:", data);
}

fetchData();

console.log("3. End");

๐Ÿงช Output:


1. Start 
2. Inside async function 
3. End
4. After await 
5. Data: { ... }

๐Ÿ” Why this output?


๐Ÿ“Œ So what makes it async?


๐Ÿ” Visualization

โ†’ Start Program 
โ†’ Run until await 
โ†’ Pause function 
โ†’ Continue with rest of the code 
โ†’ When Promise resolves 
โ†’ Resume async function from where it left