简体   繁体   English

以这种方式使用Promise很好吗?

[英]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. 我正在使用nodejs,并且具有以下内容:一个数据库和一个模块,在其中执行数据库操作。 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? 当我有这样的事情时,这是使用诺言的正确方法吗?

If you need more information please let me know and I'll try to make everything more clearer as much as I can. 如果您需要更多信息,请告诉我,我将尽一切努力使一切变得更加清晰。

Best Regards, Victor 最好的问候,维克多

You can simplify this a little, because you can remove inner catch block, just need to return inner Promise 您可以稍微简化一下,因为您可以删除内部的catch块,只需要返回内部的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 休息看起来还可以

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 谢谢你,维克多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM