Key Concepts

Review core concepts you need to learn to master this subject

Asynchronous JavaScript function

function helloWorld() { return new Promise(resolve => { setTimeout(() => { resolve('Hello World!'); }, 2000); }); } const msg = async function() { //Async Function Expression const msg = await helloWorld(); console.log('Message:', msg); } const msg1 = async () => { //Async Arrow Function const msg = await helloWorld(); console.log('Message:', msg); } msg(); // Message: Hello World! <-- after 2 seconds msg1(); // Message: Hello World! <-- after 2 seconds

An asynchronous JavaScript function can be created with the async keyword before the function name, or before () when using the async arrow function. An async function always returns a promise.

Resolving JavaScript Promises

function helloWorld() { return new Promise(resolve => { setTimeout(() => { resolve('Hello World!'); }, 2000); }); } const msg = async function() { //Async Function Expression const msg = await helloWorld(); console.log('Message:', msg); } const msg1 = async () => { //Async Arrow Function const msg = await helloWorld(); console.log('Message:', msg); } msg(); // Message: Hello World! <-- after 2 seconds msg1(); // Message: Hello World! <-- after 2 seconds

When using JavaScript async...await, multiple asynchronous operations can run concurrently. If the resolved value is required for each promise initiated, Promise.all() can be used to retrieve the resolved value, avoiding unnecessary blocking.

Async Await Promises

function helloWorld() { return new Promise(resolve => { setTimeout(() => { resolve('Hello World!'); }, 2000); }); } const msg = async function() { //Async Function Expression const msg = await helloWorld(); console.log('Message:', msg); } const msg1 = async () => { //Async Arrow Function const msg = await helloWorld(); console.log('Message:', msg); } msg(); // Message: Hello World! <-- after 2 seconds msg1(); // Message: Hello World! <-- after 2 seconds

The JavaScript async...await syntax in ES6 offers a new way write more readable and scablable code to handle promises. It uses the same features that were already built into the JavaScript language.

Using async await syntax

function helloWorld() { return new Promise(resolve => { setTimeout(() => { resolve('Hello World!'); }, 2000); }); } const msg = async function() { //Async Function Expression const msg = await helloWorld(); console.log('Message:', msg); } const msg1 = async () => { //Async Arrow Function const msg = await helloWorld(); console.log('Message:', msg); } msg(); // Message: Hello World! <-- after 2 seconds msg1(); // Message: Hello World! <-- after 2 seconds

Constructing one or more promises or calls without await can allow multiple async functions to execute simultaneously. Through this approach, a program can take advantage of concurrency, and asynchronous actions can be initiated within an async function. Since using the await keyword halts the execution of an async function, each async function can be awaited once its value is required by program logic.

JavaScript async…await advantage

function helloWorld() { return new Promise(resolve => { setTimeout(() => { resolve('Hello World!'); }, 2000); }); } const msg = async function() { //Async Function Expression const msg = await helloWorld(); console.log('Message:', msg); } const msg1 = async () => { //Async Arrow Function const msg = await helloWorld(); console.log('Message:', msg); } msg(); // Message: Hello World! <-- after 2 seconds msg1(); // Message: Hello World! <-- after 2 seconds

The JavaScript async...await syntax allows multiple promises to be initiated and then resolved for values when required during execution of the program. As an alternate to chaining .then() functions, it offers better maintainablity of the code and a close resemblance synchronous code.

Async Function Error Handling

function helloWorld() { return new Promise(resolve => { setTimeout(() => { resolve('Hello World!'); }, 2000); }); } const msg = async function() { //Async Function Expression const msg = await helloWorld(); console.log('Message:', msg); } const msg1 = async () => { //Async Arrow Function const msg = await helloWorld(); console.log('Message:', msg); } msg(); // Message: Hello World! <-- after 2 seconds msg1(); // Message: Hello World! <-- after 2 seconds

JavaScript async functions uses try...catch statements for error handling. This method allows shared error handling for synchronous and asynchronous code.

JavaScript aysnc await operator

function helloWorld() { return new Promise(resolve => { setTimeout(() => { resolve('Hello World!'); }, 2000); }); } const msg = async function() { //Async Function Expression const msg = await helloWorld(); console.log('Message:', msg); } const msg1 = async () => { //Async Arrow Function const msg = await helloWorld(); console.log('Message:', msg); } msg(); // Message: Hello World! <-- after 2 seconds msg1(); // Message: Hello World! <-- after 2 seconds

The JavaScript async...await syntax in ES6 offers a new way write more readable and scablable code to handle promises. A JavaScript async function can contain statements preceded by an await operator. The operand of await is a promise. At an await expression, the execution of the async function is paused and waits for the operand promise to resolve. The await operator returns the promise’s resolved value. An await operand can only be used inside an async function.

Async Await
Lesson 1 of 1
  1. 1
    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 oper…
  2. 2
    The async keyword is used to write functions that handle asynchronous actions. We wrap our asynchronous logic inside a function prepended with the async keyword. Then, we invoke that function. asy…
  3. 3
    In the last exercise, we covered the async keyword. By itself, it doesn’t do much; async functions are almost always used with the additional keyword await inside the function body. The await key…
  4. 4
    We’ve seen that the await keyword halts the execution of an async function until a promise is no longer pending. Don’t forget the await keyword! It may seem obvious, but this can be a tricky mistak…
  5. 5
    The true beauty of async…await is when we have a series of asynchronous actions which depend on one another. For example, we may make a network request based on a query to a database. In that cas…
  6. 6
    When .catch() is used with a long promise chain, there is no indication of where in the chain the error was thrown. This can make debugging challenging. With async…await, we use try…catch sta…
  7. 7
    Remember that await halts the execution of our async function. This allows us to conveniently write synchronous-style code to handle dependent promises. But what if our async function contains mult…
  8. 8
    Another way to take advantage of concurrency when we have multiple promises which can be executed simultaneously is to await a Promise.all(). We can pass an array of promises as the argument to P…
  9. 9
    Awesome work getting the hang of the async…await syntax! Let’s review what you’ve learned: + async…await is syntactic sugar built on native JavaScript promises and generators. + We declare an …

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo