title: Node.js - Wait for function to finish, then process further
body: <p>I am using node.js (with express.js), I want that function first execute fully then script will execute.
  I have this code,</p>
  
  <pre><code>var reg = register(otp)
  if(reg.error){
  return res.json(error:true,message:reg.message);
  }
  </code></pre>
  
  <p>Where register(otp) is a function return json object
  like {error:true,message:'User is already registered'}</p>
  
  <p>When I run the code it logs an error,
  Cannot read error property of undefined.</p>
  
  <p>What to do now?</p>