0

I've got the following async code.

const dlcOrNot = async (file) => {
  console.log('Unpacking dlc file...')
  if (file.indexOf('.dlc') > -1) {
    await decrypt.upload(file, (err, response) => {
      const links = response.success.links;
      writeFile('urls.txt', String(links).replace(/,/g, '\n'), 'utf-8', () => {
        return 'urls.txt';
      });
    });
  } else {
    return file;
  };
};

My problem is the if statement returns undefined instantly before decrypt.upload() ran, how can I make it wait for the function inside to return its value?

Alexander Hörl
  • 578
  • 4
  • 11
  • 24
  • if `decrypt.upload` takes a callback, it will unlikely return a promise. You can only `await` expressions that return a promise though, you will need to promisify your call. You should then also promisify `writeFile` (separately) and use a second `await`. – Bergi Jan 18 '19 at 09:41

1 Answers1

0

You can only await a promise.

decrypt.upload does not return a promise.

Replace it with a promise.

This question covers converting a function which takes a callback to one which returns a promise.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335