I'm attempting to iterate over a list of xml files, convert the xml content to js objects and then grab some data from the object, ultimately generating an array with one element from each xml file.
Given the asynchronous nature of readfile, I'm trying to create promises for each file and fulfill those promises when the process is done, thus allowing my calling function to act on the array of data from my promises once the async process is complete. This is not going as planned, so I'm hoping that someone can steer me in a better direction.
Keeping in mind that I am brand new to the world of js / ts. :\
Thanks!
Calling function
getBoards(boardPath)
.then(result => {console.log(result);});
Async promises function
export function getBoards(examplesPath: string): Promise<any> {
let examplesManifestPath = path.join(examplesPath, 'manifests');
let parser = new xml2js.Parser();
let boardNames: Array<string> = [];
let files = fs.readdirSync(examplesManifestPath).filter(fn => fn.endsWith('.xml'));
let promises: any = [];
files.forEach(file => {
promises.push = new Promise(function (resolve, reject) {
fs.readFile(path.join(examplesManifestPath, file), function(err, data) {
parser.parseString(data, function (err: string, result: any) {
return result['data']['boards'][0]['board'][0]['$']['name'];
});
});
});
});
return Promise.all(promises);
}