Created
May 5, 2021 07:04
-
-
Save heinrich-ulbricht/baba3b3ab70708929cf5083ff7352483 to your computer and use it in GitHub Desktop.
Get all child terms from already retrieved list of parent terms
This file contains 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
// 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