I have run into quite an annoying issue with my Javascript code due to its synchronous nature, in short the problem is I need to dump a load of data into an array.
This array is then written directly into a JSON file.
function main(callback) {
myarrary = []
//run some SQL query via stored procedure
for (result in query) {
myarray.push({
//my data is entered here
})
}
let data = JSON.stringify({myarray: somedata}, null, 4)
fs.writeFileSync('testingjson.json', data)
callback();
}
main(another_function)
My issue is that the data dump and callback are executed immediately, as such no data is transferred to the file and 'another_function` relies on data being there so that doesn't work either.
Therefore I need the dump to wait for the loop to finish appending myarray
I have also tried entering the dump and callback into my for loop however they execute on the first iteration.
Any help is appreciated!