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

91
Visualizações
Node.JS: how to wait for a process to finish before continuing?

I am new to node and stuck with this issue. Here' the file: I am running 'startProcess' function and I want to run 'downloadFiles' and wait until it's completed and save the files before executing any code after it.

This code always ends up running 'runVideoUploadEngine' even before the download has been completed?

const downloadAndSaveFiles = async ({ url, dir }) => {
  try {
    https.get(url, (res) => {
      // File will be stored at this path
      console.log('dir: ', dir);
      var filePath = fs.createWriteStream(dir);
      res.pipe(filePath);
      filePath.on('finish', () => {
        filePath.close();
        console.log('Download Completed');
      });
    });
    return true;
  } catch (e) {
    console.log(e);
    throw e;
  }
};

const downloadFiles = async ({ data }) => {
  try {
    mediaUrl = data.mediaUrl;
    thumbnailUrl = data.thumbnailUrl;
    const mediaExt = path.extname(mediaUrl);
    const thumbExt = path.extname(thumbnailUrl);

    mediaDir = `${__dirname}/temp/${'media'}${mediaExt}`;
    thumbDir = `${__dirname}/temp/${'thumb'}${thumbExt}`;

    await downloadAndSaveFiles({ url: mediaUrl, dir: mediaDir });
    await downloadAndSaveFiles({ url: thumbnailUrl, dir: thumbDir });

    return { mediaDir, thumbDir };
  } catch (e) {
    console.log(e);
    throw e;
  }
};

module.exports = {
  startProcess: async ({ message }) => {
    //check if message is proper
    data = JSON.parse(message.Body);

    //download video and thumbnail and store in temp.
    console.log('starting download..');
    const { mediaDir, thumbDir } = await downloadFiles({ data });

    console.log('dir:- ', mediaDir, thumbDir);

    pageAccessToken =
      'myRandomToken';
    _pageId = 'myRandomPageID';
    console.log('running engine');
    await runVideoUploadEngine({ pageAccessToken, _pageId, mediaDir, thumbDir });

    //start videoUploadEngine
    //on success: delete video/thumbnail
  },
};

What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

downloadAndSaveFiles returns a promise (because the function is async) but that promise doesn't "wait" for https.get or fs.createWriteStream to finish, and therefore none of the code that calls downloadAndSaveFiles can properly "wait".

If you interact with callback APIs you cannot really use async/await. You have to create the promise manually. For example:

const downloadAndSaveFiles = ({ url, dir }) => {
  return new Promise((resolve, reject) => {
    // TODO: Error handling
    https.get(url, (res) => {
      // File will be stored at this path
      console.log('dir: ', dir);
      var filePath = fs.createWriteStream(dir);
      filePath.on('finish', () => {
        filePath.close();
        console.log('Download Completed');
        resolve(); // resolve promise once everything is done
      });
      res.pipe(filePath);
    });
  });
};
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