Learn

Often in web development, we need to handle asynchronous actions— actions we can wait on while moving on to other tasks. We make requests to networks, databases, or any number of similar operations. JavaScript is non-blocking: instead of stopping the execution of code while it waits, JavaScript uses an event-loop which allows it to efficiently execute other tasks while it awaits the completion of these asynchronous actions.

Originally, JavaScript used callback functions to handle asynchronous actions. The problem with callbacks is that they encourage complexly nested code which quickly becomes difficult to read, debug, and scale. With ES6, JavaScript integrated native promises which allow us to write significantly more readable code. JavaScript is continually improving, and ES8 provides a new syntax for handling our asynchronous action, async...await. The async...await syntax allows us to write asynchronous code that reads similarly to traditional synchronous, imperative programs.

The async...await syntax is syntactic sugar— it doesn’t introduce new functionality into the language, but rather introduces a new syntax for using promises and generators. Both of these were already built in to the language. Despite this, async...await powerfully improves the readability and scalability of our code. Let’s learn how to use it!

Instructions

1.

In this lesson, you’ll be given a code-editor to write your code and a bash terminal to execute your code. To execute your program, you’ll type node app.js in the terminal and hit enter (or return). You’ll press the “Check Work” button to submit your code for evaluation.

Check out the code we provided. It shows three ways of accomplishing the same task: reading and printing from two files in a specified order:

  • The first version uses callback functions.
  • The second version uses native promise syntax
  • The third version uses async...await.

Don’t worry about understanding the async...await syntax— that’s what you’ll learn in this lesson! If you haven’t already, type node app.js into the terminal and press enter. Then, press “Check Work”.

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?