0 day streak
javascript / beginner
Snippet

Fetching Data with Async and Await

Async and Await provide a clean way to handle asynchronous operations like API requests. In Next.js, this is the standard way to retrieve data from external sources or internal API routes.

snippet.js
javascript
1
2
3
4
5
async function loadProfile() {
const response = await fetch('/api/profile');
const data = await response.json();
return data;
}
nextjs
Breakdown
1
async function loadProfile()
Defines a function that will handle asynchronous tasks.
2
await fetch('/api/profile')
Pauses execution until the network request is complete.