title: Node js async/await promise problem with ytdl and ffmpeg
body: <p>I built a simple youtube downloader cli. It looks like this (without any arg parsing for easier reproduction):</p>
  <pre><code>const ytdl = require('ytdl-core');
  const config = require('config');
  const progressBar = require('./progressBar');
  const logger = require('./logger');
  const ffmpeg = require('fluent-ffmpeg');
  
  const url = 'https://www.youtube.com/watch?v=Np8ibIagn3M';
  
  const getInfo = async (url) =&gt; {
      logger.info(`Getting metadata for ${url}`);
      const response = await ytdl.getBasicInfo(url);
      const info = response.videoDetails.title;
      logger.info(`Title: ${info}`);
      return info;
  };
  
  const getStream = async (url) =&gt; {
      logger.info(`Downloading from ${url} ...`);
  
      let allReceived = false;
      return new Promise((resolve, reject) =&gt; {
          const stream = ytdl(url, {
              quality: 'highest',
              filter: (format) =&gt; format.container === 'mp4',
          })
              .on('progress', (_, totalDownloaded, total) =&gt; {
                  if (!allReceived) {
                      progressBar.start(total, 0, {
                          mbTotal: (total / 1024 / 1024).toFixed(2),
                          mbValue: 0,
                      });
                      allReceived = true;
                  }
                  progressBar.increment();
                  progressBar.update(totalDownloaded, {
                      mbValue: (totalDownloaded / 1024 / 1024).toFixed(2),
                  });
              })
              .on('end', () =&gt; {
                  progressBar.stop();
                  logger.info('Successfully downloaded the stream!');
              });
          return resolve(stream);
      });
  };
  
  const convertToMp3 = async (stream, title) =&gt; {
      return new Promise((resolve, reject) =&gt; {
          ffmpeg({ source: stream })
              .output(`${config.get('audioDir')}/${title}.mp3`)
              .audioBitrate('192k')
              .run();
          return resolve();
      });
  };
  
  const main = async (url) =&gt; {
      const info = await getInfo(url);
      console.log('here 1');
      const stream = await getStream(url);
      console.log('here 2');
      await convertToMp3(stream, info);
      console.log('done');
  };
  
  main(url);
  </code></pre>
  <p>The output looks like:</p>
  <pre><code>➜ node youtube.js
  info:    Getting metadata for https://www.youtube.com/watch?v=Np8ibIagn3M
  info:    Title: Tu boca -  (Bachata Remix Dj Khalid)
  here 1
  info:    Downloading from https://www.youtube.com/watch?v=Np8ibIagn3M ...
  here 2
  done
  [Progress] [████████████████████████████████████████] 100% | Downloaded: 7.15/7.15 MB | Elapsed Time: 5s
  info:    Successfully downloaded the stream!
  </code></pre>
  <p>However, I would expect this output:</p>
  <pre><code>➜ node youtube.js
  info:    Getting metadata for https://www.youtube.com/watch?v=Np8ibIagn3M
  info:    Title: Tu boca -  (Bachata Remix Dj Khalid)
  here 1
  info:    Downloading from https://www.youtube.com/watch?v=Np8ibIagn3M ...
  [Progress] [████████████████████████████████████████] 100% | Downloaded: 7.15/7.15 MB | Elapsed Time: 5s
  here 2
  info:    Successfully downloaded the stream!
  done
  </code></pre>
  <p>I think I have troubles to understand async/await. As far as I understood, promisifying a function allows me wait for the result. However, it seems that it does not work. I do not know why and how to properly debug it.</p>
  <p>EDITED:</p>
  <pre><code>const getStream = async (url) =&gt; {
      logger.info(`Downloading from ${url} ...`);
  
      let allReceived = false;
      return new Promise((resolve, reject) =&gt; {
          const stream = ytdl(url, {
              quality: 'highest',
              filter: (format) =&gt; format.container === 'mp4',
          })
              .on('progress', (_, totalDownloaded, total) =&gt; {
                  console.log('totalDownloaded: ' + totalDownloaded);
                  if (!allReceived) {
                      console.log('total: ' + total);
                      progressBar.start(total, 0, {
                          mbTotal: (total / 1024 / 1024).toFixed(2),
                          mbValue: 0,
                      });
                      allReceived = true;
                  }
                  progressBar.increment();
                  progressBar.update(totalDownloaded, {
                      mbValue: (totalDownloaded / 1024 / 1024).toFixed(2),
                  });
              })
              .on('end', () =&gt; {
                  progressBar.stop();
                  logger.info('Successfully downloaded the stream!');
                  resolve(stream);
              });
      });
  };
  </code></pre>
  <p>But now it is like this:</p>
  <pre><code>➜ node youtube.js
  info:    Getting metadata for https://www.youtube.com/watch?v=Np8ibIagn3M
  info:    Title: Tu boca -  (Bachata Remix Dj Khalid)
  here 1
  info:    Downloading from https://www.youtube.com/watch?v=Np8ibIagn3M ...
  [Progress] [██████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 14% | Downloaded: 1.02/7.15 MB | Elapsed Time: 52s
  </code></pre>
  <p>Added console.log:</p>
  <pre><code>totalDownloaded: 16384
  total: 7493903
  [Progress] [░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 0% | Downloaded: 0/7.15 MB | Elapsed Time: 0stotalDownloaded: 32768
  totalDownloaded: 49152
  totalDownloaded: 65536
  totalDownloaded: 81920
  totalDownloaded: 98304
  totalDownloaded: 114688
  totalDownloaded: 131072
  totalDownloaded: 147456
  totalDownloaded: 163840
  totalDownloaded: 180224
  totalDownloaded: 196608
  totalDownloaded: 212992
  totalDownloaded: 229376
  totalDownloaded: 245760
  totalDownloaded: 262144
  totalDownloaded: 278528
  totalDownloaded: 294912
  totalDownloaded: 311296
  totalDownloaded: 327680
  totalDownloaded: 344064
  totalDownloaded: 360448
  totalDownloaded: 376832
  totalDownloaded: 393216
  totalDownloaded: 409600
  totalDownloaded: 425984
  totalDownloaded: 442368
  totalDownloaded: 458752
  totalDownloaded: 475136
  totalDownloaded: 491520
  totalDownloaded: 507904
  totalDownloaded: 524288
  totalDownloaded: 540672
  totalDownloaded: 557056
  totalDownloaded: 573440
  totalDownloaded: 589824
  totalDownloaded: 606208
  totalDownloaded: 622592
  totalDownloaded: 638976
  totalDownloaded: 655360
  totalDownloaded: 671744
  totalDownloaded: 688128
  totalDownloaded: 704512
  totalDownloaded: 720896
  totalDownloaded: 737280
  totalDownloaded: 753664
  totalDownloaded: 770048
  totalDownloaded: 786432
  totalDownloaded: 802816
  totalDownloaded: 819200
  totalDownloaded: 835584
  totalDownloaded: 851968
  totalDownloaded: 868352
  totalDownloaded: 884736
  totalDownloaded: 901120
  totalDownloaded: 917504
  totalDownloaded: 933888
  totalDownloaded: 950272
  totalDownloaded: 966656
  totalDownloaded: 983040
  totalDownloaded: 999424
  totalDownloaded: 1015808
  totalDownloaded: 1032192
  totalDownloaded: 1048576
  totalDownloaded: 1064960
  [Progress] [██████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░] 14% | Downloaded: 1.02/7.15 MB | Elapsed Time: 25s
  </code></pre>