// Asynchronous function to fetch data from an API
async function fetchData() {
// Fetch data from the specified URL
const response = await fetch('https://api.example.com/data');
// Parse the JSON data from the response
const data = await response.json();
// Return the parsed data
return data;
}
// Call the asynchronous function and handle the returned Promise
fetchData().then((data) => {
// Log the retrieved data to the console
console.log(data);
});
code
Asynchronous Fetch in JavaScript using async/await
This JavaScript code demonstrates asynchronous fetching of data from an API using the async/await syntax with the Fetch API. The comments provide explanations for each part of the code.
Fetch API
Promise
async/await