title: download and untar file than check the content, async await problem, node.js
body: <p>I am downloading a file in <code>tar</code> format with <a href="https://www.npmjs.com/package/request-promise" rel="nofollow noreferrer">request-promise</a> module. Then I untar that file with <a href="https://www.npmjs.com/package/tar" rel="nofollow noreferrer">tar</a> module using <code>async await</code> syntax. </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 list = new Promise(async (resolve, reject) =&gt; {
     const filePath = "somedir/myFile.tar.gz";
     if (!fs.existsSync(filePath)) {
       const options = {
          uri: "http://tarFileUrl",
          encoding: "binary"
       };
  
       try {
         console.log("download and untar");
  
         const response = await rp.get(options);
         const file = await fs.createWriteStream(filePath);
         file.write(response, 'binary');
         file.on('finish', () =&gt; {
           console.log('wrote all data to file');
           //here is the untar process
           tar.x(
             {
               file: filePath,
               cwd: "lists"
             }
           );
           console.log("extracted");
         });
         file.end();
       } catch(e) {
         reject();
       }
       console.log("doesn't exist");
    }
  }
  
  //here I am checking if the file exists no need to download either extract it (the try catch block)
  //then the Array is created which includes the the list content line by line
  
  if (fs.existsSync(filePath)) {
    const file = await fs.readFileSync("lists/alreadyExtractedFile.list").toString().match(/[^\r\n]+/g);
  
    if (file) {
      file.map(name =&gt; {
        if (name === checkingName) {
          blackListed = true;
          return resolve(blackListed);
        }
      });
    }
    else {
      console.log("err");
    }
  }</code></pre>
  </div>
  </div>
  </p>
  
  <p>The <code>console.log</code> output sequence is like so:</p>
  
  <blockquote>
    <p>download and untar <br>
    file doesn't exist <br>
    UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open '...lists/alreadyExtractedFile.list' <br>
    wrote all data to file <br>
    extracted</p>
  </blockquote>
  
  <p>So the file <code>lists/alreadyExtractedFile.list</code> is being checked before it's created. My guess is I am doing some wrong <code>async await</code> actions. As console.logs pointed that out the second checking block is somehow coming earlier than the file creating and untaring processes. <br>
  Please help me to figure out what I am doing wrong. </p>