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

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

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 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