title: Execution order in API's done in node js
body: <p>I'm creating an API on Node with MongoDB, I'm learning slowly. When creating a GET route, I'm experiencing a problem, the request response is sent before going through the implemented logic. In the code, the line <code>res.send(listClass);</code> it always is executed first, just after this the foreach is executed.</p>
  
  <pre><code>exports.getClassrooms = (req, res) =&gt; {
      const listClass = [];
      const query = {"userId": req.params.id};
      classUser.find(query, (err, result) =&gt; {
          if(result){
              result.forEach(data =&gt; {
                  classroom.findById({"_id": data.classId}, (err, doc) =&gt; {
                      if(doc){
                          listClass.push(doc);
                      }
                  });
              });
          }
          res.send(listClass); 
      }).catch( err =&gt; {
          res.status(500).send({
             message: err.message || "Erro to find the class."
          });
      });
  }
  </code></pre>