I am trying to recursively get all comments on hacker history using their Firebase API. The story has a kids property, which is an array of identifiers representing comments. Each comment can have its own kids property, which indicates its comments on children, etc. I want to create an array of the entire comment tree, something similar:
[{ 'title': 'comment 1', 'replies': [{ 'title': 'comment 1.1' }, { 'title': 'comment 1.2' 'replies': [{ 'title': 'comment 1.2.1' }] }] }]
I thought I could do this using the following function:
function getItem(id) { return api .child(`item/${id}`) .once('value') .then((snapshot) => { // this is a Firebase Promise let val = snapshot.val() if (val.kids) { val.replies = val.kids.map((id) => getItem(id)) } return val }) }
And then get notified after receiving the whole comment tree with:
getItem(storyId) .then(story => { // The story and all of its comments should now be loaded console.log(story) })
As a result, Promise.all().then() triggered after resolving the first level of the promises comment (which makes sense since all commentPromises resolved.) However, I want to know as soon as all the nested promises have resolved. How can i do this?
source share