简体   繁体   English

ES6嵌套承诺

[英]ES6 nested promises

I'm using promises and I am stuck with an issue, that is more a question of Best Practice. 我正在使用Promise,但遇到了一个问题,那就是最佳实践的问题。

I have a function that returns a promises resolving into an object (validated object) : 我有一个函数,它返回一个可解析为对象(已验证对象)的promise:

validate(req.body, bodyValidationSchema)

I have another function, creating an object in the database from the validated data and returning a promise resolving into the created object : 我还有另一个功能,从已验证的数据在数据库中创建一个对象,然后将一个promise解析为创建的对象:

db.model.create(validated_data, other_parameters)

However, I can't just chain those functions using them such as : 但是,我不能只使用它们来链接这些功能,例如:

validate(req.body, bodyValidationSchema)
.then(validated_data => db.model.create(validated_data, other_parameters))
.then(console.log)

Because, the last line will not print the created_object but a Promise resolving into the created object. 因为,最后一行不会打印created_object,而是将Promise解析为创建的对象。 Therefore I have to do something like this, nesting the promises : 因此,我必须做这样的事情,嵌套诺言:

validate(req.body, bodyValidationSchema)
.then(validated_data =>
  db.model.create(validated_data, other_parameters)
  .then(console.log)
)

Is there any better way to do that ? 有什么更好的方法吗? Also, if I replace the console.log by an async task and add another ".then()" not after that task but after the bigger one, it will not wait for that last task (I don't know if that is very clear...) 另外,如果我用一个异步任务替换console.log,而不是在该任务之后但在更大的任务之后添加另一个“ .then()”,它将不会等待最后一个任务(我不知道那是否非常明确...)

Thank you very much, Giltho 非常感谢吉尔托

EDIT : Here is the actual code that is showing issues 编辑 :这是显示问题的实际代码

function createEmailView(req, res) {
  validate(req.body, emailCreationSchema)
  .then(validatedBody =>
    db.Email.create({ email: validatedBody.email, userLogin: req.user.login }))
  .then(email => validate(email, emailSchema, { stripUnknown: true }))
  .then((validatedEmail) => { console.log(validatedEmail); return validatedEmail; })
  .then((validatedEmail) => {
    res.status(201).json(validatedEmail);
  })
  .catch((error) => {
    if (error instanceof Sequelize.ValidationError || error.isJoi) {
      res.status(400).json(makeStandardErrorOfValidationError(error));
    } else {
      res.status(500).json(error);
    }
  });
}

If I try printing the "email" on the 3rd line, it is not a plain object but a promise resolving into one. 如果我尝试在第三行打印“电子邮件”,则它不是一个普通的对象,而是一个可以分解为一个对象的承诺。 Therefore the validation doesn't work and just strips everything off... 因此验证不起作用,只是剥离了所有内容...

Well, sorry about this but in the end I just mistook a Sequelize Model instance for a Promise. 好吧,对此感到抱歉,但是最后我只是将Sequelize Model实例误认为是Promise。 I didn't have a promise given in parameters, therefore everything was good I just had to take the plain object from that instance 我没有在参数中给出承诺,因此一切都很好,我只需要从该实例中获取普通对象

email.nodeValues

Thanks to everyone who helped 谢谢大家的帮助

How about this? 这个怎么样?

validate(req.body, bodyValidationSchema)
.then(validated_data => {
    return db.model.create(validated_data, other_parameters);
})
.then(console.log)

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

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