Choose Language

Apply โฑ 2 min

Async Await in 100 Seconds

What You Will Learn

  • Understand the basics of the event loop and its role in asynchronous programming
  • Learn how to use promises and async/await to write efficient asynchronous code
  • Master error handling and concurrency in async/await

Key Concepts

The event loop is a single-threaded loop that runs in the browser or Node.js, executing synchronous code and queuing up asynchronous events. Macro tasks, such as set timeouts, are executed on the next event loop, while micro tasks, like fulfilled promises, are called back before the start of the next event loop. Promises are a way to handle asynchronous operations, allowing for chaining and error handling. Async/await is syntactic sugar that makes asynchronous code look like synchronous code, using the async keyword to return a promise and the await keyword to pause execution until a promise is resolved.

Code Examples

console.log('first line');
setTimeout(() => console.log('second line'), 0);
Promise.resolve().then(() => console.log('third line'));
console.log('fourth line');
// This code demonstrates the priority of the micro task queue over the macro task queue.
fetch('https://api.example.com/data')
  .then(response => response.json())
  .then(data => console.log(data))
  .catch(error => console.error(error));
// This code shows how to consume a promise-based API and handle errors.
async function getFruit(name) {
  // simulate a promise-based API
  const fruits = {
    apple: '๐ŸŽ',
    banana: '๐ŸŒ',
  };
  return fruits[name];
}

async function makeSmoothie() {
  const a = await getFruit('apple');
  const b = await getFruit('banana');
  return [a, b];
}
// This code demonstrates the use of async/await to write asynchronous code that looks like synchronous code.

Lesson Summary

In this lesson, we learned about the event loop and its role in asynchronous programming. We saw how promises are used to handle asynchronous operations and how async/await is used to write efficient asynchronous code. We also learned about error handling and concurrency in async/await. The event loop is a single-threaded loop that runs in the browser or Node.js, executing synchronous code and queuing up asynchronous events. Promises are a way to handle asynchronous operations, allowing for chaining and error handling. Async/await is syntactic sugar that makes asynchronous code look like synchronous code. By using async/await, we can write asynchronous code that is easier to read and maintain.

Practice Exercise

Write an async function that retrieves a user’s profile data from an API and then uses that data to retrieve a list of the user’s friends. Use async/await to write the code and handle any errors that may occur.

What Is Next

In the next lesson, we will learn about advanced topics in async/await, including using async/await with loops and conditional statements. We will also learn about best practices for using async/await in real-world applications.