I've defined a function to delete a file, to include in teardown for testing.
The delete function works fine, but it hangs forcing a CTRL-C to end the script, and
deleteFile('somefile.txt').then(x => console.log(x)
shows undefined
while it's waiting.
Still a novice JavaScript self-learner. No idea what I'm missing here:
async function deleteFile(file) {
let result
try {
fs.unlink(file, (err) => {
if (err) throw err;
result = `Deleted ${file}`
})
return result
} catch (err) {
return err
}
}
UPDATE I made the overarching problem I was trying to solve extra complicated. This function isn't necessary. I thought I needed to have a delete function to export into my test. A bit of a convoluted approach since I can simply directly delete the file in the my setup of my test.js.
Also there was an import of an open database handle that was not closing that was causing the script to hang.
Overall this situation was a mess, but I had some assistance in troubleshooting beyond the question posed in this post.
So, this was more an exercise in recognizing how I can make mistakes by being hyper-focused on methodology and losing track of the simple solution.
Part of learning is making wrong turns...from which one can easily recover.
Net learning: Slow down. Keep it simple. Don't hyper-focus.