I've got the following function, which doesn't execute in the order I'd like it to.
The console.log within the callback actually gets fired before the loop within the function itself even executes. So to sum it up I'd like the function "getFiles" to fully finish before the next one "createDatabase" starts to execute.
Thanks for your help. I'm already going nuts about this issue :(
const fs = require("fs")
let data = []
const getFiles = () => {
fs.readdir("./database/test/", (err, files) => {
files.forEach(file => {
require("../database/test/" + file)
data.push(Number(file.split(".")[0]))
console.log(data)
})
})
}
const createDatabase = () => {
data.sort((a, b) => {
return a-b
})
console.log(data)
// Do some other stuff
}
const run = async () => {
await getFiles
await createDatabase()
process.exitCode = 0
}
run()