Skip to content

Instantly share code, notes, and snippets.

@RomanTurner
Created September 2, 2021 21:52
Show Gist options
  • Save RomanTurner/c0e6246f6689f7ff2b7392fa08954491 to your computer and use it in GitHub Desktop.
Save RomanTurner/c0e6246f6689f7ff2b7392fa08954491 to your computer and use it in GitHub Desktop.
Nom Nom
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