这是一个"递延反模式"? [英] Is this a "Deferred Antipattern"?

查看:202
本文介绍了这是一个"递延反模式"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现很难理解递延反模式。我想我明白它主要的,但我还没有看到一个超级简单的例子,什么是服务,具有递延承诺和一个与反模式,所以我想我会试着让我自己但看到我是如何不超在知道这件事我会先得到一些澄清。

我有下面的一个工厂(SomeFactory):

  // URL ='data.json';返回{
    的getData:功能(){
        变种推迟= $ q.defer();        $ http.get(destinationFactory.url)
            。然后(功能(响应){                如果(typeof运算response.data ==='对象'){
                    deferred.resolve(response.data);
                }其他{
                    返回deferred.reject(response.data);
                }
            })            .catch(功能(错误){
            deferred.reject(错误);
        });        返回deferred.promise;
    }

我检查的对象的原因仅仅是添加验证一个简单的一层到 $ http.get()

和下面,在我的指令:

  this.var = SomeFactory.getData()
    。然后(功能(响应){
        //一些变量=响应;
    })
    .catch(功能(响应){
        //做的错误处理
});

现在我uderstanding,这是一个反模式。因为原来的递延承诺捕获错误并简单地吞下它。它不会返回错误,所以当这个的getData的方法被称为我做的是另抓抢错误。

如果这不是一个反模式,那么有人可以解释为什么都需要一个回调不爽?当我第一次开始写这个工厂/指令我预期的有什么地方做了递延承诺,但我没想到不必 .catch()两侧(又名我是那种想我能得到工厂返回响应或错误,如果我做了一个 SomeFactory.getData()


解决方案

  

这是一个延迟反模式?


是的。 延迟反模式发生时创建新的冗余递延对象从承诺链内得到解决。在你的情况,你正在使用$ q返回的东西,隐式返回一个承诺的承诺。你已经有一个承诺对象( $ http服务本身返回),所以你只需要回吧!

下面是超级简单的例子,什么是服务,具有递延承诺和一个与反模式的样子,

这是反模式

  app.factory(SomeFactory,['$ HTTP,$ Q']){
    返回{
        的getData:功能(){
            变种推迟= $ q.defer();            $ http.get(destinationFactory.url)
                .success(功能(响应){
                        deferred.resolve(response.data);
                })
                .error(功能(错误){
                deferred.reject(错误);
            });            返回deferred.promise;
        }
}

这是你应该做的。

  app.factory(SomeFactory,['$ HTTP']){
    返回{
        的getData:功能(){
           // $ HTTP本身返回一个承诺
            返回$ http.get(destinationFactory.url);
        }
}

而两者以相同的方式被消耗掉。

  this.var = SomeFactory.getData()
    。然后(功能(响应){
        //一些变量=响应;
    },函数(响应){
        //做的错误处理
});

有没有错,要么例子(ATLEAST语法)..但redundant..and不需要第一个!

希望它能帮助:)

I'm finding it hard to understand the "deferred antipattern". I think I understand it in principal but I haven't seen a super simple example of what a service, with a differed promise and one with antipattern, so I figured I'd try and make my own but seeing as how I'm not super in the know about it I'd get some clarification first.

I have the below in a factory (SomeFactory):

//url = 'data.json';

return {
    getData: function(){
        var deferred = $q.defer();

        $http.get(destinationFactory.url)
            .then(function (response) {

                if (typeof response.data === 'object') {
                    deferred.resolve(response.data);
                } else {
                    return deferred.reject(response.data);
                }
            })

            .catch(function (error) {
            deferred.reject(error);
        });

        return deferred.promise;
    }

The reason I am checking its an object is just to add a simple layer of validation onto the $http.get()

And below, in my directive:

this.var = SomeFactory.getData()
    .then(function(response) {
        //some variable = response;
    })
    .catch(function(response) {
        //Do error handling here
});

Now to my uderstanding, this is an antipattern. Because the original deferred promise catches the error and simply swallows it. It doesn't return the error so when this "getData" method is called I have do another catch to grab the error.

If this is NOT an antipattern, then can someone explain why both require a "callback" of sorts? When I first started writing this factory/directive I anticipated having to do a deffered promise somewhere, but I didn't anticipate having to .catch() on both sides (aka I was sort of thinking I could get the factory to return the response or the error if I did a SomeFactory.getData()

解决方案

Is this a "Deferred Antipattern"?

Yes it is. 'Deferred anti-pattern' happens when new redundant deferred object is created to be resolved from inside an promise chain. In your case you are using $q to return a promise for something that implicitly returns a promise. You already have a Promise object($http service itself returns a promise), so you just need to return it!

Here's the super simple example of what a service, with a differed promise and one with antipattern look like,

This is anti-pattern

app.factory("SomeFactory",['$http','$q']){
    return {
        getData: function(){
            var deferred = $q.defer();

            $http.get(destinationFactory.url)
                .success(function (response) {        
                        deferred.resolve(response.data);
                })
                .error(function (error) {
                deferred.reject(error);
            });

            return deferred.promise;
        }
}

This is what you should do

app.factory("SomeFactory",['$http']){
    return {
        getData: function(){
           //$http itself returns a promise 
            return $http.get(destinationFactory.url);
        }
}

while both of them are consumed in the same way.

this.var = SomeFactory.getData()
    .then(function(response) {
        //some variable = response;
    },function(response) {
        //Do error handling here
});

There's nothing wrong with either examples(atleast syntactically)..but first one is redundant..and not needed!

Hope it helps :)

这篇关于这是一个"递延反模式"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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