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

lvarayut

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. However, I had to write the catch functions multiple times anyway. What's the good approach to avoid duplicate catch functions? Any help would be appreciated.

Lorenz

You can actually just return user.saveAsync(). Then your error propagates to the lower catch function. 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. 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.

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

What is the proper way to catch an error and halt a program?

What is a proper way to create a JS promise that is rejected?

What's the proper way to structure this set of try catch blocks with respect to variable initializations?

What is the proper way to use Promise.reject with javascript

What's the proper way to setup an Android PreferenceFragment?

What's the proper way to share the interface?

What's the proper way to check if a constant is defined?

What's the proper way to document callbacks with jsdoc?

What's the proper way of passing a ref to a prop?

What's the proper way to recurse in LLVM assembly?

What's the proper way to link to a route with parameters?

What's the proper way of Naming a class?

What's the proper way to reraise a custom exception?

What's the proper way to write a haskell function

What's the proper way of changing the color of SetDlgItemText?

What's the proper way to use Coroutines in Activity?

What's the proper way to make the AppBar fixed?

What is the correct way to propagate errors?

php catch post proper way

Proper way to loop for a promise response

What's the proper way of using Swift's defer

MVVM - What's the proper way to communicate between views in WPF

What's the proper way to parse a very large JSON file in Ruby?

In BeautifulSoup, what's the proper way to use a strainer with lxml parsing?

What's the proper way to set up my path in crontab?

What's the proper way to close a piped readable stream?

What's the proper way to store this data in a MySQL schema?

What's the proper way of using property descriptors in Object.create?

What's the proper way to test multiple constructors in Junit?