Skip to content

Async Functions

An async function is a function that always returns a Promise. It allows you to use the await keyword inside it.

// An async function always returns a Promise
async function getData() {
return 'Hello';
}
console.log(getData()); // Promise { 'Hello' }
getData().then(console.log); // 'Hello'
const getData = async () => {
return 'Hello';
};
// With params
const fetchUser = async (id) => {
const response = await fetch(`/api/users/${id}`);
return response.json();
};
flowchart TD
A["async function fetchData()"]
B["await fetch('/api/data')"]
C["⏳ Pauses here until Promise resolves"]
D["Continues with result"]
E["Returns a Promise automatically"]
A --> B
B --> C
C --> D
D --> E
  • async keyword makes a function return a Promise
  • Works with function declarations, expressions, and arrow functions
  • Inside an async function, you can use await
  • The returned value is automatically wrapped in a resolved Promise