I need to return the boolean result of a file readAsText promise to an array filter method. The promise returns the correct value, based on the inputs. But I do not know how to get a value for the “return” function. First is the called function, followed by the array filter code segment. The ??? on the “return” indicates the value I need to fill in, for the filter method to work. If I replace the ??? manually with true or false, the filter method works fine. So how do I get the “found” variable out of the promise and into the “???” value?
phraseFound(path: string, phrase: string) {
let panelMediaFile = 'panelMedia_' + this.global.langAbbv + '.json'; // e.g. "panelMedia_en.json"
//
return this.file.readAsText(this.global.fileSystem.int + path, panelMediaFile).then((jsonStr) => {
let found = (jsonStr.toLowerCase().indexOf(phrase) == -1) ? false : true;
return found; //found = TRUE
}).catch((err) => { this.global.showError('t10', err); });
}
filteredItems = filteredItems.filter( item => {
let tapaSrcPath = this.global.path.titles + item.titleURI + '/tapaSrc/';
let found = this.phraseFound(tapaSrcPath, searchTermsPhrase)
.then(found => {
return found;
});
return ???;
});
Thank you in advance!
P.S. Here is the complete method:
searchSortTapaskills(searchTerms: string, isFilter: boolean): any {
searchTerms = (searchTerms == undefined) ? '' : searchTerms;
this.searchFiltered = (searchTerms != '');
//
let array: any = this.global.tapaskills;
if(!array) return [ ]; // if empty list, then return an empty list
let filteredItems = [ ];
//
if(this.searchFiltered) { // filter first for performance of sort
searchTerms = searchTerms.toLowerCase();
filteredItems = array;
if (isFilter) {
let searchTermsPhrase = searchTerms; // for efficiency, only search for entire phrase
filteredItems = filteredItems.filter( item => {
let tapaSrcPath = this.global.path.titles + item.titleURI + '/tapaSrc/';
let found = this.phraseFound(tapaSrcPath, searchTermsPhrase)
.then(found => {
return found; //found = TRUE
});
return found; //found = promise object
});
} else {
let searchTermsArr: string[ ] = searchTerms.split(' '); // space delimited
for (let idx=0; idx < searchTermsArr.length; idx++) { // loop thru all words
filteredItems = filteredItems.filter( item => {
return item.title.toLowerCase().includes(searchTermsArr[idx]); // only filter the title
});
};
}; //...(2019-04-18)//
} else {
filteredItems = array;
};
//
let sortColumn = this.sortColumn;
let sortOrder = this.sortOrder;
//
return filteredItems.sort(function(a, b){ // finally sort what remains
let property = sortColumn;
if(a[property] < b[property]){
return -1 * sortOrder;
}
else if( a[property] > b[property]){
return 1 * sortOrder;
}
else{
return 0;
}
});
} //...(2019-04-13)//