多个'单angularjs。那么()'的承诺 - 都使用_original_数据 [英] multiple `.then()`s on single angularjs promise -- all use _original_ data

查看:100
本文介绍了多个'单angularjs。那么()'的承诺 - 都使用_original_数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个应用angularjs依靠诺言,虽然它的工作,我不知道如果我能做到这一点更优化。

I'm writing an angularjs app relying on promises, and though it's working, I wonder if I could do it more optimally.

在code的开始,我创建熄灭获取一些数据的承诺。当这样做时,我想运行多种功能,所有使用该数据。该功能安装在应用程序中不相关的部分,所以我不知道在哪个它们附着在承诺的顺序。它们并不需要按顺序进行任

At the beginning of the code, I'm creating a promise that goes off to fetch some data. When this is done, I want to run several functions that all use this data. The functions are attached at unrelated parts of the app, so I do not know the order in which they're attached to the promise. They do not need to be done in sequence either.

app.service("Fetch", function ($q){
    return function() {
        var def = $q.defer();
        somelibrary.asynccall(function(error, data){ //callback
            if (error) def.reject(error);
            else def.resolve(data);
        });
        return def.promise;
    };
});

app.controller("ctrl", function ($scope, Fetch) {
    var prom = Fetch();

    //somewhere:
    prom.then(function(data){$scope.var1 = data["VAR1"];});
    //somewhere else:
    prom.then(function(data){$scope.var2 = data["VAR2"]});
});

这里的主要缺点是,在后面然后每当preceding那些完成,这是没有必要在这里s的仅执行。

The main disadvantage here is that the later thens are only executed whenever the preceding ones are finished, which is not necessary here.

另外,我需要添加返回数据每个函数内(数据){...} ,以下则()不具有数据可用。

Also, I need to add return data inside every function(data){...}, otherwise the following then() does not have the data available.

不是有另一种方式来做到这一点,更容易出现这种情况?

Is there not another way to do this, more apt for this situation?

编辑:按@ jfriend00,我错了提及;事实上2功能都运行,平行,只要许被成功地解决,并且它们不链接并因此不依赖于彼此。感谢您的帮助。

as mentioned by @jfriend00, I was mistaken; in fact the 2 functions both run, in parallel, as soon as the promise is successfully resolved, and they are not chained and therefore not dependent on each other. Thanks for your help

推荐答案

把我的意见变成一个答案,因为它似乎清理问题:

通过你的模式,两个。然后()呼吁同样的承诺将要被调用此起彼伏时的承诺得到解决。第二个。然后()只有做与原来的承诺,并没有任何关系的第一个。然后()

With your pattern, the two .then() calls on the same promise are going to get called one after another when the promise is resolved. The second .then() has only to do with the original promise and nothing to do with what happens on the first .then().

这些不是链接所以第二个。然后()有哪些时,从第一回不依赖。然后(),都将被传递相同的数据。他们是同样的承诺有点像两个事件处理程序侦听同一事件的多只观察家。

These are not chained so the second .then() has no dependency upon what is returned from the first .then() and both will be passed the same data. They are just multiple watchers of the same promise kind of like two event handlers listening for the same event.

这是不变的诺言两个。然后()处理程序将他们连接到承诺,都将被传递相同的数据的顺序被调用。

The two .then() handlers on the same promise will be called in the order they were attached to the promise and will both be passed the same data.

这篇关于多个'单angularjs。那么()'的承诺 - 都使用_original_数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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