title: Proper usage of promise await and async function
body: <pre><code>router.post("/register", function (req, res) {
  
      console.log(req.body);
  
      // generate a salt
      (async function(){
  
  
      const salt = await bcrypt.genSalt(10);
  
      // hash the password along with our new salt
      const hash = await bcrypt.hash(req.body.txtPassword1, salt);
      return hash;
      })().then((data)=&gt;{
          var txtPassword = data;
          let newUser = new userModel({
              userName: req.body.txtUserName,
              email: req.body.txtEmail,
              profilePic: req.body.txtFileUpload,
              password: txtPassword,
              isAdmin: false
  
          });
          newUser.save((function (err) {
              if (err) {
                  console.log("failed to save the new User ! : ", err);
  
              } else {
                  console.log("New user has been added successfully with Id", newUser._id);
              }
          }))
  
          req.flash('success', 'Registeration Successful');
  
          console.log("Session value ", req.session);
          console.log("value of txt password =&gt; ", txtPassword)
          res.render("blogHome", { title: "Blogs || Home" });
      });
  
  
  }); 
  </code></pre>
  
  <p>I want to know if this is the proper way to use await.
  I had to resort to this way because when I simply tried using </p>
  
  <pre><code>var hash = await bcrypt.hash(req.body.txtPassword1,salt);
  </code></pre>
  
  <p>I was getting unexpected identifier error when I went with above code and when I googled I found out that 
  await have to be used inside a async function so I wrapped the whole thing in side a IIFE and used normal promise handling with .then()
  But I feel unknowingly I have complicated a simple thing .
  Can anyone point out the simplest way to do this.
  The reason I was forced to use promise was because due to asynchronous execution
  database save statement always executed before the hash was calculated and it would mean password is empty which in turn triggers schema validation for password field </p>