title: node js callback as var
body: <p>Am trying to create a method that i can call from all parts of my express app, i simply pass a value to this method and it searches through mongodb using moongose to see if user exists, then it returns a Boolean as result. </p>
  
  <pre><code>function findUserExists(name){
  
   User.findOne({ "name": name }, function(err, user) {
  
   if (user){ 
      console.log("user exits");
      return true;
   }
   else{ 
          console.log("user not exits");
      return false; }
  });
  </code></pre>
  
  <p>Now when i call it in another method like this, it doesn't return the Boolean value in time.</p>
  
  <pre><code>if (findUserExists("username")){
  
  // redirect to dashboard
  console.log("user exits");
      res.redirect('/dashboard');
  }
  else {
   // redirect to sign up page
  console.log("user must sign up");
      res.redirect('/signup');
  }
  </code></pre>
  
  <p>So in this case the other functions run before getting the Boolean value from the findUserExists(), my question is how can i make this function return a value before other functions run, in a way that i can call it in an IF condition or even a var like this</p>
  
  <pre><code>var exists = findUserExists(name);
  </code></pre>