-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
24 lines (22 loc) · 765 Bytes
/
api.js
File metadata and controls
24 lines (22 loc) · 765 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function fetchWeatherData(apiKey, location) {
const url = `https://api.openweathermap.org/data/2.5/weather?q=${location}&appid=${apiKey}&units=metric`;
return fetch(url)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
return {
temperature: data.main.temp,
description: data.weather[0].description,
city: data.name,
country: data.sys.country
};
})
.catch(error => {
console.error('There was a problem with the fetch operation:', error);
});
}
export { fetchWeatherData };