What is `async`/`await`?
`async` and `await` are used in JavaScript to handle asynchronous operations. These keywords allow you to write asynchronous code in a way that looks and behaves like synchronous code.
Understanding `async`
An `async` function is a function that always returns a `Promise`. It allows you to use `await` inside the function, which pauses the function execution until the promise is resolved.
async function fetchData() { /* fetch data */ }
Understanding `await`
The `await` keyword can only be used inside an `async` function. It pauses the execution of the function until the promise is resolved or rejected.
let result = await fetch('https://api.example.com/data');
Example: Fetching Data from an API
In the example below, we will fetch data from a simulated API using `async`/`await` and display the result.