Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!