什么时候 .then(success, fail) 被认为是 promise 的反模式? [英] When is .then(success, fail) considered an antipattern for promises?

查看:26
本文介绍了什么时候 .then(success, fail) 被认为是 promise 的反模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了(推荐阅读!).

//some_promise_call().then(logger.log).catch(logger.log)尝试 {var 结果 = some_call();logger.log(结果);}赶上(e){logger.log(e);}

catch 记录器还将处理来自成功记录器调用的异常.

差别就这么多.

<块引用>

我不太明白它对 try 和 catch 的解释

争论点是,通常,您希望在处理的每个步骤中捕获错误,并且不应在链中使用它.期望您只有一个最终处理程序来处理所有错误 - 而当您使用反模式"时,不会处理某些 then-callbacks 中的错误.

然而,这个模式实际上非常有用:当你想要处理恰好在这一步发生的错误,并且你想要在没有发生错误的情况下做一些完全不同的 - 即当错误发生时不可恢复.请注意,这是分支您的控制流.当然,这有时是需要的.


<块引用>

以下有什么问题?

some_promise_call().then(function(res) { logger.log(res) }, function(err) { logger.log(err) })

你必须重复你的回调.你宁愿

some_promise_call().catch(函数(e){返回 e;//没关系,我们就记录一下}).完成(功能(资源){logger.log(res);});

您也可以考虑使用 .finally()为此.

I had a look at the bluebird promise FAQ, in which it mentions that .then(success, fail) is an antipattern. I don't quite understand its explanation as for the try and catch. What's wrong with the following?

some_promise_call()
.then(function(res) { logger.log(res) }, function(err) { logger.log(err) })

It seems that the example is suggesting the following to be the correct way.

some_promise_call()
.then(function(res) { logger.log(res) })
.catch(function(err) { logger.log(err) })

What's the difference?

解决方案

What's the difference?

The .then() call will return a promise that will be rejected in case the callback throws an error. This means, when your success logger fails, the error would be passed to the following .catch() callback, but not to the fail callback that goes alongside success.

Here's a control flow diagram:

To express it 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);
}

The second log (which is like the first argument to .then()) will only be executed in the case that no exception happened. The labelled block and the break statement feel a bit odd, this is actually what python has try-except-else for (recommended reading!).

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

The catch logger will also handle exceptions from the success logger call.

So much for the difference.

I don't quite understand its explanation as for the try and catch

The argument is that usually, you want to catch errors in every step of the processing and that you shouldn't use it in chains. The expectation is that you only have one final handler which handles all errors - while, when you use the "antipattern", errors in some of the then-callbacks are not handled.

However, this pattern is actually very useful: When you want to handle errors that happened in exactly this step, and you want to do something entirely different when no error happened - i.e. when the error is unrecoverable. Be aware that this is branching your control flow. Of course, this is sometimes desired.


What's wrong with the following?

some_promise_call()
.then(function(res) { logger.log(res) }, function(err) { logger.log(err) })

That you had to repeat your callback. You rather want

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

You also might consider using .finally() for this.

这篇关于什么时候 .then(success, fail) 被认为是 promise 的反模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆