title: YouTube Downloader using node.js
body: <p>So I'm creating YouTube Downloader using node.js. The problem is the files are already created after I ran the code, but the files are 0kb and it prints Successfully. What I want is the program must be print successfully when I successfully download the video, also must not be created the file yet. the file must be created after the one video successfully downloaded</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const playlist = [
    {
      title: "What is DevOps",
      videoUrl: "https://www.youtube.com/watch?v=mBBgRdlC4sc",
    },
    {
      title: "Introduction To DevOps ",
      videoId: "Me3ea4nUt0U",
      videoUrl: "https://www.youtube.com/watch?v=Me3ea4nUt0U",
    },
    {
      title: "DevOps Tutorial For Beginners ",
      videoId: "YSkDtQ2RA_c",
      videoUrl: "https://www.youtube.com/watch?v=YSkDtQ2RA_c",
    },
  ];
  const fs = require("fs");
  const ytdl = require("ytdl-core");
  const length = playlist.length;
  
  playlist.forEach((pl, i) =&gt; {
    const { videoUrl, title } = pl;
    const item = i + 1;
  
    ytdl(videoUrl, {
      format: "mp4",
    }).pipe(fs.createWriteStream(`${title}.mp4`));
    console.log(`${item}/${length} - ${title} downloaded successfully`);
  });</code></pre>
  </div>
  </div>
  </p>