Victor
Victor

Reputation: 478

Is it good to use Promise this way?

I'm trying to understand and master promise but I ran into an issue which I'm not sure if this would be the right way to do things.

I'm using nodejs and I have the following: A database and a module in which I do something with the database. At one point I'm trying to do create a new user in the database and after the user is created to create another entry in another table for that user with other details.

passport.use('local-signup', new LocalStrategy({
    usernameField: 'username',
    passwordField: 'password',
    
  },
  function(username, password, done) {
    
    // Before creating the user access Sequlize beforeCreate method so we can encrypt the password
    User.beforeCreate(function(req) {
      console.log('Encryptying password for user ',req.username)
      return encryptPass(req.password)
      .then(success => {
        req.password = success;
      })
      .catch(err => {
        if (err) console.error(err);
      });
    });
    
    User.create({
      username: username,
      password: password
    }).then(function(createdUser) {
      console.log(createdUser);
      UserDetails.create({
        id:createdUser.dataValues.id
      }).then((data)=>{
        console.log(data);
        return done(null,createdUser);
      }).catch((error)=>{
        console.log(error)
        return done(error)
      })
    })
    .catch(function(error) {
      console.error(error)
      return done(`${error.message}.`);
    });
  }
));

Is this the correct way to use the promises when I have something like this?

Upvotes: 0

Views: 98

Answers (2)

Victor
Victor

Reputation: 478

Thank you all, based on comments I have ended up with the below code. As much as I tried to avoid callback hell issue it still looks a bit like it but since both promises needs to be called I think it's a good solution and not nesting it to much:

 // Local sign-up strategy
  passport.use('local-signup', new LocalStrategy({
    usernameField: 'username',
    passwordField: 'password',

  },
  function(username, password, done) {

    // Before creating the user access Sequlize beforeCreate method so we can encrypt the password
    User.beforeCreate(function(req) {
      console.log('Encryptying password for user ',req.username)
      return encryptPass(req.password)
      .then(success => {
        req.password = success;
      })
      .catch(err => {
        if (err) console.error(err);
      });
    });

    User.create({
      username: username,
      password: password
    }).then(function(createdUser) {
      console.log(createdUser);
      return createdUser;
    }).then((userData) =>{
      UserDetails.create({
        id:userData.dataValues.id
      }).then((userDetails)=>{
        return done(null,userData)
      })
    }).catch(function(error) {
      console.error(error)
      return done(`${error.message}.`);
    });
  }
));

Thank you, Victor

Upvotes: 0

ponury-kostek
ponury-kostek

Reputation: 8060

You can simplify this a little, because you can remove inner catch block, just need to return inner Promise

User.create({
    username: username,
    password: password
}).then(function (createdUser) {
    console.log(createdUser);
    return UserDetails.create({
        id: createdUser.dataValues.id
    }).then((data) => {
        console.log(data);
        return done(null, createdUser);
    })
}).catch(function (error) {
    console.error(error);
    return done(`${error.message}.`);
});

Rest looks OK

Upvotes: 1

Related Questions