× logo Home HTML CSS Javascript React-App Top-10 Javascript Algorithms logo
logo

JavaScript async/await



JavaScript async/await
We use the async keyword with a function to represent that the function is an asynchronous function. The async function returns a promise.

The syntax of async function is:
async function name(parameter1, parameter2, ...paramaterN) {
// statements
}
Enter fullscreen mode Exit fullscreen mode

Here,
- name - name of the function
- parameters - parameters that are passed to the function

Example: Async Function

// async function example
async function f() {
  console.log('Async function.');
  return Promise.resolve(1);

}
f(); 
Enter fullscreen mode Exit fullscreen mode

Output
Async function. 

Enter fullscreen mode Exit fullscreen mode
In the above program, the async keyword is used before the function to represent that the function is asynchronous.

Since this function returns a promise, you can use the chaining method then() like this:

async function f() {
  console.log('Async function.');
  return Promise.resolve(1);

}
f().then(function(result) { 
  console.log(result)
});
Enter fullscreen mode Exit fullscreen mode

Output
Async function 
1 
Enter fullscreen mode Exit fullscreen mode
Halkaa kasii wada See more https://www.programiz.com/javascript/async-await

Full-Stack Engineer