Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

275
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda