Adarsh Bhadauria
Adarsh Bhadauria

Reputation: 85

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?

Upvotes: 2

Views: 2727

Answers (1)

Felix Kling
Felix Kling

Reputation: 816364

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);
    });
  });
};

Upvotes: 3

Related Questions