Created
September 2, 2021 21:52
-
-
Save RomanTurner/c0e6246f6689f7ff2b7392fa08954491 to your computer and use it in GitHub Desktop.
Nom Nom
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
const STORIES_URL = 'https://hacker-news.firebaseio.com/v0/topstories.json'; | |
const ITEMS_URL = 'https://hacker-news.firebaseio.com/v0/item/'; | |
const getStoryIds = async () => { | |
const response = await fetch(STORIES_URL); | |
if (!response.ok) { | |
const message = `An error has occured: ${response.status}`; | |
throw new Error(message); | |
} | |
const result = await response.json(); | |
return result; | |
}; | |
const getTopStories = async (ids) => { | |
const itemsResponse = await Promise.all( | |
ids.map((id) => fetch(ITEMS_URL + id + '.json')) | |
); | |
const stories = await Promise.all(itemsResponse.map((story) => story.json())); | |
return stories; | |
}; | |
const limiter = 20; | |
(async () => { | |
const ids = await getStoryIds(); | |
const stories = await getTopStories(ids.slice(0, limiter)); | |
const topStories = stories.map((story) => { | |
({ title, by, url } = story); | |
return { title, by, url }; | |
}); | |
console.log(topStories); | |
//Do stuff with topStories | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment