Member-only story
Introduction to Async and Await
In the world of JavaScript, asynchronous programming is a key concept for performing tasks that take some time to complete, like fetching data from an API or reading a file from the disk. It helps us avoid blocking the main thread, keeping our applications snappy and responsive. Two keywords that are central to asynchronous programming in Javascript are async
and await
. We are going to explore async await Javascript functionality in detail here.
The async
Keyword
The async
keyword is used to declare a function as asynchronous. It tells JavaScript that the function will handle asynchronous operations, and it will return a promise, which is an object representing the eventual completion (or failure) of an asynchronous operation.
Here’s how you can define an async function:
async function fetchData() {
// Function body here
}
The await
Keyword
The await
keyword is used inside an async function to pause the execution of the function until a promise is resolved. In simpler terms, it waits for the result of an asynchronous operation.