What is Async and Await?
What is async and await?
Handling asynchronous actions in JavaScript was originally done using callback functions. But the ES6 version of JavaScript introduced Promises and the .then()
and .catch()
syntax.
After ES6, the JavaScript language has continued to evolve and provide even more ways of handling asynchronous actions, like async
and await
. This new syntax allows developers to treat asynchronous code as if it were synchronous and write cleaner code without getting caught up in chaining .then()
s and .catch()
s. Take a look below, the first example is using the .then()
method:
myPromise().then((resolvedValue) => {console.log(resolvedValue);});
Now compare it with using async
and await
:
const myAsyncFunc = async () => {let resolvedValue = await myPromise();console.log(resolvedValue);};
Both code snippets do the same thing! The async
keyword makes the function capable of handling asynchronous code, that has the await
keyword. Both keywords are syntactic sugar — it doesn’t introduce new functionality into the language, it makes it slightly easier to write and read. In this course, Build a Professional Website with Wix, we’ll be using the .then()
and .catch()
syntax to teach asynchronicity but we still wanted to introduce async
and await
for your web development progress.
So the choice is yours. If you want to learn more about this cool ES6 feature and get into the pros and cons continue on to the lesson and quiz. Otherwise, you can go straight to learning about the backend!
Author
'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'
Meet the full teamRelated articles
Learn more on Codecademy
- Free course
Create a Professional Website with Velo by Wix
From beginners to experienced web developers, Wix offers a wide range of solutions to quickly create a website that you can proudly share.Beginner Friendly17 hours - Free course
Velo by Wix: Using Async Actions for the Backend
Integrate JavaScript asynchronous actions with Velo to create a backend.Beginner Friendly4 hours