Home > Web Front-end > JS Tutorial > When is Using `.then(success, fail)` an Antipattern in Promises?

When is Using `.then(success, fail)` an Antipattern in Promises?

Patricia Arquette
Release: 2024-12-28 18:36:13
Original
842 people have browsed it

When is Using `.then(success, fail)` an Antipattern in Promises?

.then(success, fail): When is It Considered an Antipattern for Promises?

In the Bluebird Promise FAQ, .then(success, fail) is flagged as an antipattern. This can be confusing, especially if you don't understand the explanation regarding try and catch statements.

What's the Issue with .then(success, fail)?

The .then() call returns a promise that rejects if the callback throws an error. This means if the success logger fails, the error would be passed to the following .catch() callback, not the fail callback accompanying success.

Control Flow Diagram

The control flow for .then with two arguments and .then-catch chain is illustrated below:

[Image of control flow diagrams for .then and .then-catch chain]

Synchronous Code Equivalent

To express this in synchronous code:

// some_promise_call().then(logger.log, logger.log)
then: {
    try {
        var results = some_call();
    } catch(e) {
        logger.log(e);
        break then;
    } // else
        logger.log(results);
}
Copy after login

.then(success) vs. .then().catch()

Compare this with:

// some_promise_call().then(logger.log).catch(logger.log)
try {
    var results = some_call();
    logger.log(results);
} catch(e) {
    logger.log(e);
}
Copy after login

In this case, the catch logger handles exceptions from the success logger call.

when is .then(success, fail) an Antipattern?

The argument against this pattern is that you should generally catch errors in every processing step and avoid using it in chains. The expectation is that you have only one final handler for all errors.

However, this pattern is helpful when handling specific errors in a particular step and taking a different action if no error occurs. Be aware that this branches your control flow.

Improved Antipattern Example

Consider the following:

some_promise_call()
   .catch(function(e) {
       return e; // it's OK, we'll just log it
   })
   .done(function(res) {
       logger.log(res);
   });
Copy after login

Alternatively, you could use .finally() for this purpose.

The above is the detailed content of When is Using `.then(success, fail)` an Antipattern in Promises?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template