I have an async method from a package module with a callback function. The method itself returns undefined.
I've very new to async coding and would like to be able to halt the execution of the program until a variable arrayWithData is assigned data from the callback method.
e.g.
let dataFiles = fs.etc
function makeDataArray(dataFiles) {
let arrayWithData = [];
for (let dataFile in dataFiles) {
package.magicMethodToParseData(dataFile, function(result){ //async method with callback
arrayWithData.push(result) //happens later
});
}
return arrayWithData; //This returns undefined
}
function doSomethingWithData(dataArray) {
/* Doing Stuff with Data Array */
}
/* Broken Code with undefined */
doSomethingWithData( makeDataArray(dataFiles) );
I know I can just add the rest of my execution inside the callback function but want to avoid that and keep the code flat.
How can I wait for data to be assigned to the array and then continue execution?
Update: Added github tester project to showcase the full problem as the promise keeps getting rejected.. Github Link: https://github.com/Jonathan002/autofont.git
dataFilesan array? Perhaps you meant to usefor ofinstead offor in(for inis for iterating the keys of objects).