Reputation: 239
I try to build a keyword list from all existing keywords. I manage to retrieve them all and output them in the debug console.
I am confused about how and when to call resolve(taxonomyKeywords)
. I like to call it when all loops and their inner loops are finished. I tried using foreach
, yet run in the same issue. Is my approach way off and there's a much easier way?
private searchKeyword(searchTerm: string, results: ITag[]) : ITag[] | Promise<ITag[]>
{
return new Promise<IPersonaProps[]>( (resolve, reject) => {
let taxonomyKeywords : ITag[] = [];
this.props.taxonomyProvider.getTermStores().then( (stores: ITermStore[]) => {
for(var store of stores )
{
this.props.taxonomyProvider.getTermGroups( store.id ).then( (groups: ITermGroup[]) => {
for(var group of groups)
{
this.props.taxonomyProvider.getTermSets(group).then( (sets: ITermSet[]) => {
for(var termSet of sets)
{
this.props.taxonomyProvider.getTerms(termSet).then( (terms: ITerm[]) => {
for(var term of terms)
{
if( term.name.indexOf(searchTerm) >= 0 )
{
taxonomyKeywords.push( { key: term.name, name: term.name} );
}}});}});}});}});});}
Upvotes: 0
Views: 70
Reputation: 4604
I think below pattern shall be better:
private function searchKeyword(searchTerm: string, results: ITag[]): ITag[] | Promise<ITag[]> {
return this.props.taxonomyProvider
.getTermStores()
.then((stores: ITermStore[]) => {
return Promise.all(stores.map(s => this.props.taxonomyProvider.getTermGroups(s.id)));
})
.then((groups: Array<ITermGroup[]>) => {
const flattenGroups: ITermGroup[] = groups.reduce((p, c) => p.concat(c), []);
return Promise.all(flattenGroups.map(g => this.props.taxonomyProvider.getTermSets(g)));
})
.then((sets: Array<ITermSet[]>) => {
const flattenSets: ITermSet[] = sets.reduce((p, c) => p.concat(c), []);
return Promise.all(flattenSets.map(s => this.props.taxonomyProvider.getTerms(s)));
})
.then((terms: Array<ITerm[]) => {
const flattenTerms: ITerm[] = terms.reduce((p, c) => p.concat(c), []);
return flattenTerms
.filter(t => t.name.indexOf(searchTerm) >= 0)
.map(t => ({ key: t.name, name: t.name }));
});
}
You should learn how promise
work
Upvotes: 1