title: How to add await in a for loop to wait for function call?
body: <p>im a beginner in Javascript / Nodejs.</p>
  <p>i want to read a folder, and then each by each call a function with await...</p>
  <p>I have a Folder, inside that folder i have Images.
  with readdir i get all Folder Images with extension.</p>
  <p>with that code i read the folder and split the ImageName and the .png, so i have only the ImageName without the .png.</p>
  <p>idk if there is a better solution.</p>
  <pre><code>fs.readdir(testFolder, (err, files) =&gt; {
    files.forEach(file =&gt; {
     const split = file.split('.');
     .....split[0]
    });
  });
  </code></pre>
  <p>If i add then this code inside the readdir</p>
  <pre><code>storeAsset();
  </code></pre>
  <p>Code from storeAsset</p>
  <pre><code>async function storeAsset() {
     const client = new NFTStorage({ token: NFT_STORAGE_API_KEY })
     const metadata = await client.store({
         name: 'ExampleNFT',
         description: 'My ExampleNFT is an awesome artwork!',
         image: new File(
             [await fs.promises.readFile('assets/Test.png')],
             'MyExampleNFT.png',
             { type: 'image/png' }
         ),
     })
     console.log(&quot;Metadata stored on Filecoin and IPFS with URL:&quot;, metadata.url)
  }
  </code></pre>
  <p>The Function storeAsset() run without Problem but its not waiting to finish StoreAsset and then each by each.</p>
  <p>If i Add await that the forEach loop wait for finish each File...;</p>
  <pre><code>await storeAsset();
  </code></pre>
  <p>i get this Message:</p>
  <pre><code>await storeAsset();
      ^^^^^
  
  SyntaxError: Unexpected reserved word
  ←[90m    at ESMLoader.moduleStrategy (node:internal/modules/esm/translators:115:18)←[39m
  ←[90m    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:289:14)←[39m
  ←[90m    at async link (node:internal/modules/esm/module_job:70:21)←[39m
  </code></pre>
  <p>So, how i can modify the readdir function with await storeasset that every function call wait for finish and then the next one?</p>
  <p>thanks for helping</p>
  <p>edit:</p>
  <p>i have this now:</p>
  <pre><code>    fs.readdir(testFolder, (err, files) =&gt; {
    files.forEach(async file =&gt; {
     const split = file.split('.');
  //
      await storeAsset();
  //
     console.log(split[0]);
      //   process.exit(1);
    });
  });
  </code></pre>
  <p>but its a bit wrong...</p>
  <pre><code>Metadata stored on Filecoin and IPFS with URL: ipfs://xxx/metadata.json
  4
  Metadata stored on Filecoin and IPFS with URL: ipfs://xxx/metadata.json
  5
  Metadata stored on Filecoin and IPFS with URL: ipfs://xxx/metadata.json
  1
  Metadata stored on Filecoin and IPFS with URL: ipfs://xxx/metadata.json
  3
  Metadata stored on Filecoin and IPFS with URL: ipfs://xxx/metadata.json
  2
  </code></pre>
  <p>I need: 1 Image &gt; Upload wait for Return Answer, then 2 Image and so on, and not start every upload at same time and then the upload is mixxed and not 1,2,3,4,5...</p>