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

107
Visualizações
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 à 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