Skip to content

Instantly share code, notes, and snippets.

@heinrich-ulbricht
Created May 5, 2021 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heinrich-ulbricht/baba3b3ab70708929cf5083ff7352483 to your computer and use it in GitHub Desktop.
Save heinrich-ulbricht/baba3b3ab70708929cf5083ff7352483 to your computer and use it in GitHub Desktop.
Get all child terms from already retrieved list of parent terms
// note: in a previous step "parentTerms" has already been retrieved; now we want all child terms down the hierarchy
const childTermsPromise = (parentTerms: ((ITermData & ITerm)[])): Promise<(ITermData & ITerm)[]> => {
const termsWithChildTerms = [...parentTerms.filter(term => term.TermsCount > 0)];
if (termsWithChildTerms.length == 0) {
return Promise.resolve(parentTerms);
}
// get all child terms
const childPromises = termsWithChildTerms.map(childTerms => childTerms.terms.get().then(values => childTermsPromise(values)));
// wait for child terms and concat with parent terms
return Promise.all(childPromises).then(result => result.reduce((prev, current) => prev.concat(current), [])).then(r => r.concat(parentTerms));
};
return childTermsPromise(alreadyRetrievedParentTerms);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment