Created
April 10, 2024 20:00
-
-
Save helabenkhalfallah/7c5a1d8896e4a56a4b3cf9ce1c57c299 to your computer and use it in GitHub Desktop.
Reusing Api Declaration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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