
- Javascript Basics Tutorial
- Javascript - Home
- JavaScript - Roadmap
- JavaScript - Overview
- JavaScript - Features
- JavaScript - Enabling
- JavaScript - Placement
- JavaScript - Syntax
- JavaScript - Hello World
- JavaScript - Console.log()
- JavaScript - Comments
- JavaScript - Variables
- JavaScript - let Statement
- JavaScript - Constants
- JavaScript - Data Types
- JavaScript - Type Conversions
- JavaScript - Strict Mode
- JavaScript - Reserved Keywords
- JavaScript Operators
- JavaScript - Operators
- JavaScript - Arithmetic Operators
- JavaScript - Comparison Operators
- JavaScript - Logical Operators
- JavaScript - Bitwise Operators
- JavaScript - Assignment Operators
- JavaScript - Conditional Operators
- JavaScript - typeof Operator
- JavaScript - Nullish Coalescing Operator
- JavaScript - Safe Assignment Operator
- JavaScript - Delete Operator
- JavaScript - Comma Operator
- JavaScript - Grouping Operator
- JavaScript - Yield Operator
- JavaScript - Spread Operator
- JavaScript - Exponentiation Operator
- JavaScript - Operator Precedence
- JavaScript Control Flow
- JavaScript - If...Else
- JavaScript - While Loop
- JavaScript - For Loop
- JavaScript - For...in
- Javascript - For...of
- JavaScript - Loop Control
- JavaScript - Break Statement
- JavaScript - Continue Statement
- JavaScript - Switch Case
- JavaScript - User Defined Iterators
- JavaScript Functions
- JavaScript - Functions
- JavaScript - Function Expressions
- JavaScript - Function Parameters
- JavaScript - Default Parameters
- JavaScript - Function() Constructor
- JavaScript - Function Hoisting
- JavaScript - Self-Invoking Functions
- JavaScript - Arrow Functions
- JavaScript - Function Invocation
- JavaScript - Function call()
- JavaScript - Function apply()
- JavaScript - Function bind()
- JavaScript - Closures
- JavaScript - Variable Scope
- JavaScript - Global Variables
- JavaScript - Smart Function Parameters
- JavaScript Objects
- JavaScript - Number
- JavaScript - Boolean
- JavaScript - Strings
- JavaScript - Arrays
- JavaScript - Date
- JavaScript - DataView
- JavaScript - Handler
- JavaScript - Math
- JavaScript - RegExp
- JavaScript - Symbol
- JavaScript - Sets
- JavaScript - WeakSet
- JavaScript - Maps
- JavaScript - WeakMap
- JavaScript - Iterables
- JavaScript - Reflect
- JavaScript - TypedArray
- JavaScript - Template Literals
- JavaScript - Tagged Templates
- Object Oriented JavaScript
- JavaScript - Objects
- JavaScript - Classes
- JavaScript - Object Properties
- JavaScript - Object Methods
- JavaScript - Static Methods
- JavaScript - Display Objects
- JavaScript - Object Accessors
- JavaScript - Object Constructors
- JavaScript - Native Prototypes
- JavaScript - ES5 Object Methods
- JavaScript - Encapsulation
- JavaScript - Inheritance
- JavaScript - Abstraction
- JavaScript - Polymorphism
- JavaScript - Destructuring
- JavaScript - Destructuring Assignment
- JavaScript - Object Destructuring
- JavaScript - Array Destructuring
- JavaScript - Nested Destructuring
- JavaScript - Optional Chaining
- JavaScript - Global Object
- JavaScript - Mixins
- JavaScript - Proxies
- JavaScript Versions
- JavaScript - History
- JavaScript - Versions
- JavaScript - ES5
- JavaScript - ES6
- ECMAScript 2016
- ECMAScript 2017
- ECMAScript 2018
- ECMAScript 2019
- ECMAScript 2020
- ECMAScript 2021
- ECMAScript 2022
- JavaScript Asynchronous
- JavaScript - Asynchronous
- JavaScript - Callback Functions
- JavaScript - Promises
- JavaScript - Async/Await
- JavaScript - Microtasks
- JavaScript - Promisification
- JavaScript - Promises Chaining
- JavaScript - Timing Events
- JavaScript - setTimeout()
- JavaScript - setInterval()
- JavaScript Cookies
- JavaScript - Cookies
- JavaScript - Cookie Attributes
- JavaScript - Deleting Cookies
- JavaScript Browser BOM
- JavaScript - Browser Object Model
- JavaScript - Window Object
- JavaScript - Document Object
- JavaScript - Screen Object
- JavaScript - History Object
- JavaScript - Navigator Object
- JavaScript - Location Object
- JavaScript - Console Object
- JavaScript Web APIs
- JavaScript - Web API
- JavaScript - History API
- JavaScript - Storage API
- JavaScript - Forms API
- JavaScript - Worker API
- JavaScript - Fetch API
- JavaScript - Geolocation API
- JavaScript Events
- JavaScript - Events
- JavaScript - DOM Events
- JavaScript - addEventListener()
- JavaScript - Mouse Events
- JavaScript - Keyboard Events
- JavaScript - Form Events
- JavaScript - Window/Document Events
- JavaScript - Event Delegation
- JavaScript - Event Bubbling
- JavaScript - Event Capturing
- JavaScript - Custom Events
- JavaScript Error Handling
- JavaScript - Error Handling
- JavaScript - try...catch
- JavaScript - Debugging
- JavaScript - Custom Errors
- JavaScript - Extending Errors
- JavaScript Important Keywords
- JavaScript - this Keyword
- JavaScript - void Keyword
- JavaScript - new Keyword
- JavaScript - var Keyword
- JavaScript HTML DOM
- JavaScript - HTML DOM
- JavaScript - DOM Methods & Properties
- JavaScript - DOM Document
- JavaScript - DOM Elements
- JavaScript - DOM Attributes (Attr)
- JavaScript - DOM Forms
- JavaScript - Changing HTML
- JavaScript - Changing CSS
- JavaScript - DOM Animation
- JavaScript - DOM Navigation
- JavaScript - DOM Collections
- JavaScript - DOM NodeList
- JavaScript - DOM DOMTokenList
- JavaScript Advanced Chapters
- JavaScript - Bubble Sort Algorithm
- JavaScript - Circular Reference Error
- JavaScript - Code Testing with Jest
- JavaScript - CORS Handling
- JavaScript - Data Analysis
- JavaScript - Dead Zone
- JavaScript - Design Patterns
- JavaScript - Engine and Runtime
- JavaScript - Execution Context
- JavaScript - Function Composition
- JavaScript - Immutability
- JavaScript - Kaboom.js
- JavaScript - Lexical Scope
- JavaScript - Local Storage
- JavaScript - Memoization
- JavaScript - Minifying JS
- JavaScript - Mutability vs Immutability
- JavaScript - Package Manager
- JavaScript - Parse S-Expressions
- JavaScript - Prototypal Inheritance
- JavaScript - Reactivity
- JavaScript - Require Function
- JavaScript - Selection API
- JavaScript - Session Storage
- JavaScript - SQL CRUD Operations
- JavaScript - Supercharged Sorts
- JavaScript - Temporal Dead Zone
- JavaScript - Throttling
- JavaScript - TRPC Library
- JavaScript - Truthy and Falsy Values
- JavaScript - Upload Files
- JavaScript - Date Comparison
- JavaScript - Recursion
- JavaScript - Data Structures
- JavaScript - Base64 Encoding
- JavaScript - Callback Function
- JavaScript - Current Date/Time
- JavaScript - Date Validation
- JavaScript - Filter Method
- JavaScript - Generating Colors
- JavaScript - HTTP Requests
- JavaScript - Insertion Sort
- JavaScript - Lazy Loading
- JavaScript - Linked List
- JavaScript - Nested Loop
- JavaScript - Null Checking
- JavaScript - Get Current URL
- JavaScript - Graph Algorithms
- JavaScript - Higher Order Functions
- JavaScript - Empty String Check
- JavaScript - Form Handling
- JavaScript - Functional Programming
- JavaScript - Parameters vs Arguments
- JavaScript - Prototype
- JavaScript - Reactive Programming
- JavaScript - Reduce Method
- JavaScript - Rest Operator
- JavaScript - Short Circuiting
- JavaScript - Undefined Check
- JavaScript - Unit Testing
- JavaScript - Validate URL
- JavaScript Miscellaneous
- JavaScript - Ajax
- JavaScript - Async Iteration
- JavaScript - Atomics Objects
- JavaScript - Rest Parameter
- JavaScript - Page Redirect
- JavaScript - Dialog Boxes
- JavaScript - Page Printing
- JavaScript - Validations
- JavaScript - Animation
- JavaScript - Multimedia
- JavaScript - Image Map
- JavaScript - Browsers
- JavaScript - JSON
- JavaScript - Multiline Strings
- JavaScript - Date Formats
- JavaScript - Get Date Methods
- JavaScript - Set Date Methods
- JavaScript - Modules
- JavaScript - Dynamic Imports
- JavaScript - BigInt
- JavaScript - Blob
- JavaScript - Unicode
- JavaScript - Shallow Copy
- JavaScript - Call Stack
- JavaScript - Reference Type
- JavaScript - IndexedDB
- JavaScript - Clickjacking Attack
- JavaScript - Currying
- JavaScript - Graphics
- JavaScript - Canvas
- JavaScript - Debouncing
- JavaScript - Performance
- JavaScript - Style Guide
JavaScript - Async/Await
The JavaScript functions defined with the async/await keyword can perform the same task as promises with fewer lines of code, and it makes the code readable. The promise's syntax is a bit complex, so the async/await syntax is introduced.
To use async/await, we need to define an aync function first. For this we write async before function definition. An async function returns a promise. The await keyword is used inside an async function only. The await keyword makes JavaScript to wait for the promise to resolve before continuing the function.
Let's understand the async/await keywords in details taking them separately −
The JavaScript Async Keyword
A JavaScript function defined with the async keyword is called the asynchronous function. The async function allows you to produce the asynchronous code.
It always returns the promise. If you don't return the promise manually and return the data, string, number, etc., it creates a new promise and resolves that promise with the returned value.
Syntax
You could use the syntax below to define a function using the async keyword in JavaScript −
async function func_name(parameters) { // function body }
In the above syntax, we have used the 'async' keyword before the function name.
Parameters
- Func_name − It is a valid identifier for the function name.
- Parameters − It takes multiple parameters, the same as a regular function.
Look at the below asynchronous function, returning the 'hello world' text. It returns the promise with the 'hello world' success message.
async function printMessage() { return "Hello World"; }
The below code is similar to the above code.
function printMessage() { return Promise.resolve("Hello World"); }
You can use the then() and catch() methods to solve the promise returned from the asynchronous function.
Example
We return the text from the getText() function in the code below.
After that, we use the then() and catch() method with the execution of the getText() method to consume the promise returned by the getText() function.
Here, you can observe that we have returned text from the asynchronous function, but it is returning the promise.
<html> <body> <div id = "output"> </div> <script> async function getText() { return "Text from the getText() function."; } getText().then((text) => { document.getElementById('output').innerHTML = text + "<br>"; }).catch((err) => { document.getElementById('output').innerHTML += JSON.stringify(err); }); </script> </body> </html>
Output
Text from the getText() function.
JavaScript Await Keyword
You can use the await keyword inside a JavaScript asynchronous function only. It pauses the execution of the function until the promise gets settled, which means it is either rejected or fulfilled.
Syntax
Following is the syntax to use the await keyword inside an asyn function in JavaScript −
async function func_name(parameters) { await promise; // Function body }
In the above syntax, we have used the await keyword inside the async function.
Example
We have defined the solvePromise() async function in the code below. We have created the new promise using the Promise() constructor in the function. After that, we used the await keyword with the promise to resolve it rather than using the then() or catch() method.
In the output, you can observe that it prints the fulfillment message.
<html> <body> <div id = "output">The resultant value from the promise is:</div> <script> async function solvePromise() { const promise = new Promise((resolve, reject) => { resolve('Promise is solved'); }) const result = await promise; document.getElementById('output').innerHTML += result; } solvePromise(); </script> </body> </html>
Output
The resultant value from the promise is: Promise is solved
Example (Waiting for Timeout)
In the code below, we set the timeout of 2000 milliseconds using the setTimeOut() method to resolve the promise.
After that, we used the await keyword with a promise to pause the execution of the function until the promise is settled. In the output, you can see that it prints the message returned from the promise after 2 seconds.
<html> <body> <div id = "output">The promise is being solved <br> </div> <script> async function solvePromise() { const promise = new Promise((resolve, reject) => { setTimeout(() => { // Setting up timeout for promises resolve('The promise is solved after 2 seconds'); }, 2000); }) const result = await promise; document.getElementById('output').innerHTML += result; } solvePromise(); </script> </body> </html>
Output
The promise is being solved The promise is solved after 2 seconds
Error Handling with JavaScript Async/Await
While consuming the promise, we used the then() and catch() methods to handle the data and errors.
With the asynchronous function, you can use the trycatch block to handle the errors.
When the promise is fulfilled successfully, the control executes the remaining code of the try block. Otherwise, it executes the code of the catch block to fix the errors.
Syntax
Following is the syntax to handle errors in the asynchronous function −
try { const result = await promise; // Manipulate data } catch (err) { // Handle errors }
We need to consume the promise in the try block and handle the errors in the catch block. The catch() method also takes the err as a parameter, which is a promise rejection message or an error object.
Example
In the code below, we have defined a promise and rejected it.
After that, we consume the promise in the try block. As the promise is rejected, the execution control will go into the catch block and print the rejection message.
<html> <body> <div id = "demo"> </div> <script> let output = document.getElementById('demo'); async function solvePromise() { const promise = new Promise((resolve, reject) => { reject("The promise is rejected"); }) try { const result = await promise; output.innerHTML += "Inside the try block. <br>"; output.innerHTML += result; } catch (err) { output.innerHTML += "Inside the catch block. <br>"; output.innerHTML += err; } } solvePromise(); </script> </body> </html>
Output
Inside the catch block. The promise is rejected
JavaScript Async Class Methods
You can also define the asynchronous class methods using the async keyword to handle the asynchronous operations.
It has the same syntax as the asynchronous function.
Syntax
Following is the syntax to use the async/await with class methods in JavaScript −
async method_name() { return await promise; }
In the above syntax, method_name is an identifier for the class method, and it uses the async/await keyword to make the method asynchronous.
You can consume the promise returned by the method using the then() and catch() methods.
Example
In the below code, we have created the animal class.
The animal class contains the getAnimalName() method and returns the Lion text. We have used the await keyword before the Lion string, which pauses the execution of the method until the string is created. However, you can also return the promise.
After that, we use the then() method to consume the promise and print the animal name in the output.
<html> <body> <div id = "output"> </div> <script> class animal { async getAnimalName() { return await "Lion"; } } const lionObj = new animal(); lionObj.getAnimalName().then((data) => { document.getElementById('output').innerHTML = "The animal name is: " + data; }) </script> </body> </html>
Output
The animal name is: Lion
Real-time Example of JavaScript Async/Await
The above examples are basic examples to demonstrate the use of the async/await keywords in JavaScript.
Lets understand how to use the async/await in real-time development.
Example
In the code below, when the user clicks the button, it calls the getData() function.
The getData() function is an asynchronous function. We used the trycatch block inside the function to handle errors.
In the try block, we used the fetch() API to fetch the data from the API and used the await keyword.
After that, we used the json() method with the response to convert into the JSON and used the await keyword with that.
Next, we print the data.
Also, we print the error message in the catch() method.
<html> <body> <button onclick = "getData()">Get Data</button> <div id = "demo"> </div> <script> let output = document.getElementById('demo'); async function getData() { try { let response = await fetch('https://api.github.com/users'); // Pauses the execution until it gets the data let data = await response.json(); // Pauses the execution until it converts the data into json output.innerHTML += "login: " + data[0].login + "<br>"; output.innerHTML += "id: " + data[0].id + "<br>"; output.innerHTML += "node_id: " + data[0].node_id + "<br>"; output.innerHTML += "avatar_url: " + data[0].avatar_url + "<br>"; } catch (err) { output.innerHTML += "The error is: " + json.stringify(err); } } </script> </body> </html>
Output

Benefits of Using JavaScript Async Function
Here are some benefits of using the asynchronous function.
- It increases the readability of code.
- It reduces the complexity of the code.
- It can handle multiple promises easily.
- It makes debugging easier.
- You can replace the callback function and promises with the asynchronous function.