Skip to content

Instantly share code, notes, and snippets.

@d0ruk
Created July 18, 2017 20:19
Show Gist options
  • Save d0ruk/91acfe33e4c001798c8afe89a48a31da to your computer and use it in GitHub Desktop.
Save d0ruk/91acfe33e4c001798c8afe89a48a31da to your computer and use it in GitHub Desktop.
async/await recursion
import pMap from "p-map"
const endpoints = {
new: "https://hacker-news.firebaseio.com/v0/newstories.json",
top: "https://hacker-news.firebaseio.com/v0/topstories.json",
best: "https://hacker-news.firebaseio.com/v0/beststories.json",
item: id => `https://hacker-news.firebaseio.com/v0/item/${id}.json`,
user: name => `https://hacker-news.firebaseio.com/v0/user/${name}.json`,
}
function get(arr, endpoint) {
if (!Array.isArray(arr)) arr = new Array(arr);
const promises = arr.map(e => window.fetch(endpoint(e)));
return Promise.all(promises)
.then(arr => arr.map(res => res.json()))
.then(arr => Promise.all(arr))
}
const traverse = async function(id, endpoint) {
const comment = await get(id, endpoint);
if (comment.deleted || comment.dead) {
return;
} else if (!comment.kids) {
return [comment];
} else {
const promises = comment.kids.map(kid => traverse(kid, endpoint));
promises.unshift(comment);
return Promise.all(promises)
.then(arr => arr.filter(e => e))
}
}
async function crawl(elem) {
const tree = await traverse(elem, endpoints.item);
return tree;
}
export function getComments(kids) {
return pMap(kids, crawl, { concurrency: 3 });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment