title: Trying to get asynchronous functionality with glob
body: <p>I have this code:</p>
  
  <p><a href="https://i.stack.imgur.com/CzWUl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CzWUl.png" alt="enter image description here"></a></p>
  
  <p>Which receive 2 args, the first is <code>script</code>, it's only a linux command, and the other is <code>template</code>, it's only a string that gives what html template wants to use an user.</p>
  
  <p>this <code>appCreator</code> has 3 methods, the first with the comment <code>// remove template from app</code> will remove one html template from the app folder.</p>
  
  <p>Another with the comment <code>// copying template</code> only copy a template from <code>TEMPLATE_FOLDER</code> on <code>APP_FOLDER</code> folder.</p>
  
  <p>And the last one is with the comment <code>// getting all html files</code>, only find all the html on a explicite template from the app folder, and replace the text <code>PASTEROCK_SCRIPT</code> from the html, putting the script text.</p>
  
  <p>This code works... Nice, but not appropriately, because the synchronous javascript does not respect the functionality.</p>
  
  <p><a href="https://i.stack.imgur.com/uU4Lj.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uU4Lj.png" alt="enter image description here"></a></p>
  
  <p>With the numbers, you can check the way how is running... And I want to respect the order... Something like this:</p>
  
  <p><a href="https://i.stack.imgur.com/yNlTs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yNlTs.png" alt="enter image description here"></a></p>
  
  <p>How can I resolve it?</p>
  
  <p>With async and await I tried:</p>
  
  <pre><code>import * as fs from "fs";
  import * as glob from "glob";
  import { ncp } from "ncp";
  import * as rimraf from "rimraf";
  
  export default async function appCreator(script: string, template: string) {
    const APP_FOLDER = `${__dirname}/../app/${template}/`;
    const TEMPLATE_FOLDER = `${__dirname}/../template/${template}/`;
  
    // remove template from app
    if (fs.existsSync(APP_FOLDER)) {
      await rimraf.sync(APP_FOLDER);
    }
  
    // copying template
    await ncp(TEMPLATE_FOLDER, APP_FOLDER, err =&gt; {
      if (err) {
        console.log(err);
      }
    });
  
    // getting all html files
    await glob.sync(`${APP_FOLDER}**/*.html`).map(file =&gt; {
      // reading html file
      const readFile = fs.readFileSync(file, { encoding: "utf8" });
      let replace: string | number = readFile.search("PASTEROCK_SCRIPT");
  
      if (replace !== -1) {
        replace = readFile.replace("PASTEROCK_SCRIPT", script);
  
        fs.writeFileSync(file, replace, { encoding: "utf8" });
      }
    });
  }
  </code></pre>