简体   繁体   English

在承诺中传播.catch的正确方法是什么?

[英]What's the proper way to propagate .catch in promise?

I'm using bluebird to promisify the mongoose library. 我正在使用蓝鸟来宣传猫鼬图书馆。 So, I currently find and save data as following: 所以,我目前找到并保存数据如下:

    User.findOneAsync({email: req.body.text})
      .then(function(user) {
        user.saveAsync()
          .spread(function(savedUser){
            res.json(savedUser);
          })
          .catch(function(err) {
            res.json({
              status: 500,
              message: 'foo'
            });
          });
      })
      .catch(function(err) {
         res.json({
           status: 500,
           message: 'foo'
         });
      });

Two catch functions are totally identical. 两个捕获功能完全相同。 This is just an demo, I sometimes have two identical catch functions in practical work. 这只是一个演示,我有时在实际工作中有两个相同的捕获功能。 I could separate the function inside the catch into it own function. 我可以将catch中的函数分成它自己的函数。 However, I had to write the catch functions multiple times anyway. 但是,无论如何,我必须多次编写catch函数。 What's the good approach to avoid duplicate catch functions? 避免重复捕获功能的好方法是什么? Any help would be appreciated. 任何帮助,将不胜感激。

You can actually just return user.saveAsync() . 实际上你可以只返回user.saveAsync() Then your error propagates to the lower catch function. 然后您的错误传播到较低的catch函数。 Like that: 像那样:

 User.findOneAsync({email: req.body.text})
  .then(function(user) {
    return user.saveAsync()
      .spread(function(savedUser){
        res.json(savedUser);
      });
  })
  .catch(function(err) {
     res.json({
       status: 500,
       message: 'foo'
     });
  });

This works because your spread returns a Promise. 这是有效的,因为您的spread返回Promise。 That promise is then passed along the outer chain, including a possible error. 然后,这个承诺将沿着外链传递,包括可能的错误。 In the outer chain you can then catch that with your catch function which will now catch errors from the inner and outer chain since they are connected. 在外链中,您可以使用catch函数catch它,现在它将从内链和外链捕获错误,因为它们已连接。

You could also shorten this code by much and not have two promise chains by doing something along the lines of this: 您还可以通过以下方式执行某些操作来缩短此代码并且没有两个承诺链:

 User.findOneAsync({email: req.body.text})
 .call("saveAsync")
 .spread(function (savedUser) {
     res.json(savedUser);
 })
 .catch(function(err) {
    res.json({
      status: 500,
      message: 'foo'
    });
 });

This is generally considered good practice when working with promises. 在使用promises时,这通常被认为是一种很好的做法。

You should avoid nesting in then success handle unless required. 你应该避免嵌套在那么成功处理,除非需要。 It makes your code more readable and you have to use just one catch function. 它使您的代码更具可读性,您只需使用一个catch函数。 All rejected promises gets propagated to last catch function. 所有被拒绝的承诺都会传播到最后一个捕获函数。

User.findOneAsync({email: req.body.text})
    .then(function(user) {
        return user.saveAsync();
    })
    .spread(function(savedUser){
        return res.json(savedUser);
    })
    .catch(function(err) {
        return res.json({
            status: 500,
            message: 'foo'
        });
    });

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

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