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

108
Vistas
UnhandledPromiseRejection when returning promise in async-await function

I've already searched among a lot of questions asked by other users but I can't still figure out why my function doesn't work properly. I wrote this code:

async function getFile(c, sourcePath, fileName, destPath) {
    return await new Promise((resolve, reject) => {
      c.cwd(sourcePath, function(err, currentDir) { // "c" is an instance of ftp module's class, "cwd" is one of its methods to change working directory
        err = new Error('trying to throw an error for testing the func');
        if (err) reject(err);
        c.get(fileName, function(err, stream) {
          if (err) reject(err);
          stream.once('close', function() { resolve(); });
          stream.pipe(fs.createWriteStream(destPath));
        });
      });
    }));
}

(async function() {
  try {
    await getFile(c, '/path/to', 'file.xls', 'file.xls');

  } catch (err) {
    throw err;
  }
})();

Node returns an UnhandledPromiseRejectionWarning, and I don't understand why. I have returned a promise in the first function and then called that function in another one correctly structured with async-await syntax. What is wrong with my code?

about 4 years ago · Juan Pablo Isaza
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