title: email confirmation in node js
body: <p>I am writing email confirmation code in node js. Following approach I am adopting</p>
  <ul>
  <li>Generate 5 digit random number</li>
  <li>Save user data (email, password etc) along with 5 digit random number in database (mongodb)</li>
  <li>send random number on email address.</li>
  <li>apply <code>setTimeout()</code> methon to wait for 20 minutes to confirm mail address</li>
  <li>after 20 minutes if user verifies addres then it is fine otherwise remove user data from database.</li>
  </ul>
  <p>but this code goes on waiting for 20 minutes until the setTimeout period ends. is there any way to apply this strategy without waiting for 20 minutes? I know it is simple but I am not able to crack it out. Following is code...</p>
  <pre><code>const emailCode = Math.floor(Math.random()*90000)+10000;
      try
      {
          const user = new User({...req.body, emailCode});
          req.user = user;
          await user.save()
      }
      catch(err)
      {
          if(err.keyPattern)
          {
              res.status(409).send({err: &quot;User Already Exists&quot;})
          }
          else if(err.errors.email)
          {
              res.status(400).send({err: err.errors.email.message})
          }
          else if(err.errors.password)
          {
              res.status(400).send({err: err.errors.password.message})
          }
          res.status(400).send(err);
      }
      const transporter = nodemailer.createTransport({
          service: 'gmail',
          auth: {
              user: '',
              pass: ''
          }
      });
      const mailOptions = {
          from: '',
          to: req.body.email,
          subject: &quot;Confirm Your Email Address&quot;,
          text: &quot;Use the following 5 digit code to confirm your email address \n&quot;+emailCode.toString()
      };
      try
      {
          const mail = await transporter.sendMail(mailOptions);
          console.log(&quot;here&quot;);
          await new Promise(resolve =&gt;
          {
              setTimeout(resolve, 10000)
          })
          console.log(&quot;there&quot;);
          console.log(req.user.verify)
          if(!req.user.verify)
          {
              req.user.remove();
          }
      }
      catch(err)
      {
          res.send(err)
      }
  </code></pre>