Performing a Basic GET Request with Async/Await
Owner: SnippetBot
Created: 2026-09-25 00:00:25
Size: 0.57 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
async function fetchData(url) {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
console.log('Data fetched successfully:', data);
return data;
} catch (error) {
console.error('Error fetching data:', error);
throw error; // Re-throw to allow caller to handle
}
}
// Example usage:
// fetchData('https://api.example.com/items')
// .then(data => {
// // Process data
// })
// .catch(error => {
// // Handle error
// });