title: How to use await in a forEach loop in nodejs
body: <p>I have an array of users and I want to check how many of them are joined to my telegram channel and my checking method is an async method and I can use the method like this:</p>
  
  <pre><code>check(user)
      .then((res) =&gt; {
      if(res) {
         // user is joined 
        }
      else {
         // user is not joined
      }
  })
  </code></pre>
  
  <p>But I don't know how to use this method for an array of users.</p>
  
  <p>I have tested this code :</p>
  
  <pre><code>members = 0;
  users.forEach((user) =&gt; {
              check(user)
                  .then((result) =&gt; {
                      if(result) {
                            members++;
                        }
                  });
          })
  </code></pre>
  
  <p>But this code is definitely wrong because I don't know when should I send the result to my admin (the person who want to check how many users are joined). I put the sending method after the <code>forEach</code> but It shows a very low number(near 0).</p>
  
  <p>I searched and I found a keyword <code>await</code> and I tried it in an async method:</p>
  
  <pre><code>async function checkMembership() {
      let data;
      await check(user)
          .then((res) =&gt; {
              data = res
          });
      console.log(data);
  }
  </code></pre>
  
  <p>and It works well but when I use the <code>await</code> in forEach loop like this:</p>
  
  <pre><code>users.forEach((user) =&gt; {
              await check(user)
                  .then((result) =&gt; {
                      console.log(result);
                  });
          })
  </code></pre>
  
  <p>I got the following error:
  <code>SyntaxError: await is only valid in async function</code>.
  How should I handle this magical forEach loop?</p>
  
  <p>UPDATE-1:
  I have tested this code too but I got the previous error:</p>
  
  <pre><code>async function checkMembership() {
  
      User.find({}, (err, res) =&gt; {
          for(let user of res) {
              await check(user)
                  .then((ress) =&gt; {
                  console.log(ress)
                  })
          }
          console.log('for is finished');
    });
  }
  </code></pre>
  
  <p>UPDATE-2:</p>
  
  <p>This code didn't work too:</p>
  
  <pre><code>Promise.all(users.map(check))
              .then((Res) =&gt; {
                  console.log(Res);
              })
  </code></pre>
  
  <p>and I got the following error:</p>
  
  <p><code>TypeError: #&lt;Promise&gt; is not a function</code></p>