Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

274
Views
Node.js fs.unlink() executes properly but hangs. How do we end it gracefully

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

const fs = require('fs')

function deleteFile(file) {
    return new Promise((resolve, reject) => {
        fs.unlink(file, (err) => {
            if (err) reject(err);
            resolve(`Deleted ${file}`)
        })
    })
}
deleteFile(some file).then(x => console.log("res", x)).catch(err=>console.log(err.message))
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!