Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

269
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda