title: Node js, Wait, until get all data from MongoDB
body: <p>I have a problem with async function and callbacks in Node js. I need to get my friends' all posts and display it. But if i do that without setTimeout(), it returns only some part of data. How can i solve this problem without putting setTimeout? Of course it's absurd to wait for 5-6 or 10 seconds to get all data. I also tried with Promises, but again response is incomplete. Please someone can help me?</p>
  
  <pre><code>//Sending request with axios to Controller
  axios.post(packages.proxy+'users/getFriendsPosts',{id: user_id},config)
      .then(res =&gt; {
          // Code for displaying result
      })
  
  
  //User Controller
  router.post("/getFriendsPosts", getFriendsPosts);
  
  
  //Send request body to userService.js
  function getFriendsPosts(req, res, next) {
      userService.getFriendsPosts(req.body, function(posts, user){
          res.json({posts,user});
      })
          .catch(err =&gt; next(err));
  }
  
  
  //userService.js
  module.exports = {
      getFriendsPosts,
  };
  
  
  async function getFriendsPosts(user,callback){
      var arr = [];
      var array = [];
      MongoClient.connect(url, async function(errr, db) {
          if (errr) throw errr;
          var dbo = db.db("drone-x");
          //Find user
          dbo.collection("users").find({_id: ObjectId(user.id)}).toArray(async function(err, result) {
              if (err) throw err;
              result.forEach(async function(element, index) {
                  if(element.friends.length != 0){
                      element.friends.forEach(async function(elem) {
                          //Find user's friends
                          dbo.collection("users").find({_id: ObjectId(elem.id)}).toArray(async function(error, res) {
                              if (error) throw error;
                              //push user's friends to arr
                              arr.push(res);
                              res.forEach(async function(elements) {
                                  //Find user's friends posts
                                  dbo.collection("posts").find({userId: elements._id.toString()}).toArray(async function(errors, results) {
                                      if (errors) throw errors;
                                      //push user's friends posts to array
                                      array.push(results);
                                      //callback results through setTimeout
                                      setTimeout(async function(){ await callback(array, arr); db.close(); }, 2000);
  
                                  });
                              });
  
                          });
                      });
                  }
                  else
                  {
                      await callback("0");
                  }
              });
          });
  
      });
  }
  </code></pre>
  
  <p>If i don't use setTimeout function, it just returns 2-3 data, but with setTimeout, it returns all data. And if data will be raise, then i need to increase the setTimeout time. But of course it's not good idea. Someone can help me?</p>