Search code examples
node.jsbluebird

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


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

What is difference between them. Please Explain in brief.

promise()
.then()
.error() OR
.catch()

Solution

  • .catch:

    • Catches all errors and is also ES6 standard

    .error:

    • Only catches when error is instance of Error
    • is a convenience method in Bluebird

    Many libraries don't throw error which is instance of Error, so you are better off using .catch

    Some library or user code may not throw error which is instanceOf Error. Although it is recommended to throw Error (or subclass) instance but there is nothing stopping to do otherwise.

    From 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.

    Some instances where error was not instance of Error. They have been fixed.

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