JavaScript
Public
Fetch Active Users
Fetches users from an API and returns only those who are active using async/await and ES6+ syntax.
#api, fetch, async-await
JavaScript
const fetchActiveUsers = async (url) => {
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const users = await response.json();
return users.filter(user => user.active);
} catch (error) {
console.error('Failed to fetch users:', error);
return [];
}
};