- Promises are a fundamental part of JavaScript used for handling asynchronous operations. They represent an operation that hasn't completed yet but is expected in the future.
- Async/Await is a way of handling promises more conveniently. It allows you to write asynchronous code in a more synchronous, linear manner.
- Axios is a third-party library used for making HTTP requests. It utilizes promises but adds extra features for HTTP-specific needs, like automatic transformation of JSON data and simplified error handling.
Each of these tools has its specific use case and advantages, and they are often used together. For example, you might use Axios to make an HTTP request and handle the returned promise with async/await for cleaner and more readable code.

Here are examples for using a Promise, async/await, and Axios to make an HTTP request to the URL https://api.example.com/data:
1. Using a Promise with Fetch API
fetch('https://api.example.com/data')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
console.log(data);
})
.catch(error => {
console.error('Fetch error:', error);
});This example uses the Fetch API, which returns a Promise. It handles the response and converts it to JSON, then logs the data or catches any errors.
2. Using Async/Await with Fetch API
async function fetchData() {
try {
const response = await fetch('https://api.example.com/data');
if (!response.ok) {
throw new Error('Network response was not ok');
}
const data = await response.json();
console.log(data);
} catch (error) {
console.error('Fetch error:', error);
}
}
fetchData();This example uses async/await for more readable asynchronous code. It awaits the fetch request, processes the response, and handles any errors.
3. Using Axios
import axios from 'axios';
axios.get('https://api.example.com/data')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.error('Axios error:', error);
});This example uses Axios, a popular HTTP client. It simplifies the process of making requests and handling responses.
Notes:
- Error Handling: In both the Fetch API examples, it's important to check
response.okto handle HTTP errors. Axios, on the other hand, automatically throws an error for bad HTTP statuses. - JSON Parsing: Fetch API requires manual parsing of the JSON response (
response.json()), whereas Axios automatically parses the JSON response. - Axios Import: Remember to install Axios (
npm install axios) and import it in your file when using it.