Skip to content

Instantly share code, notes, and snippets.

@helabenkhalfallah
Created April 10, 2024 20:00
Show Gist options
  • Save helabenkhalfallah/7c5a1d8896e4a56a4b3cf9ce1c57c299 to your computer and use it in GitHub Desktop.
Save helabenkhalfallah/7c5a1d8896e4a56a4b3cf9ce1c57c299 to your computer and use it in GitHub Desktop.
Reusing Api Declaration
// api.js
// Import the axios library for making HTTP requests
import axios from 'axios';
// Define an asynchronous function to fetch data from the API
export async function fetchData() {
try {
// Make an HTTP GET request to fetch data from the API
const response = await axios.get('https://api.example.com/data');
// Return the response data
return response.data;
} catch (error) {
// Log an error message if there's an error fetching data
console.error('Error fetching data:', error.message);
// Rethrow the error to handle it in the component
throw error;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment