在 bluebird nodejs 中,我什么时候应该使用 .error() 而不是 .catch

in bluebird nodejs when should i use .error() instead of .catch

在 bluebird nodejs 中,我什么时候应该使用 .error() 而不是 .catch。

它们有什么区别。请简要说明。

promise()
.then()
.error() OR
.catch()
Whenever Dealing with bluebird think about the asynchronous call of 
try{
    }catch(){
    } block
    function doSomeWork() {
      return Promise.try(function() {

        return request.get(url).then(function(response) {
          // ... do some specific work
        });

      }).catch(function(err) {
        console.log("Some specific work failed", err);
        throw err; // IMPORTANT! throw unless you intend to suppress the error
      });
    }

.catch:

  • 捕获所有错误,也是 ES6 standard

.error:

  • 仅在错误是 Error
  • 的实例时捕获
  • Bluebird
  • 中的一种便捷方法

许多库不会抛出 Error 实例的错误,因此您最好使用 .catch

某些库或用户代码可能不会抛出 instanceOf Error 错误。虽然建议抛出 Error(或子类)实例,但除此之外别无他法。

来自 http://bluebirdjs.com/docs/api/catch.html

Like .catch but instead of catching all types of exceptions, it only catches operational errors. Note, "errors" mean errors, as in objects that are instanceof Error - not strings, numbers and so on. See a string is not an error.

有些实例的错误不是 Error 的实例。它们已被修复。

  1. https://github.com/NodeRedis/node_redis/issues/374
  2. https://github.com/google/google-api-nodejs-client/issues/345