可这个承诺嵌套改为链接? [英] Can this promise nesting be changed to chaining?

查看:198
本文介绍了可这个承诺嵌套改为链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是伪方案

           | then (items)          | then (items, actions)
getItems() | getActions(for:items) | apply(actions -> items)
 :promise  |  :promise             | model <= items
           |                       |  :synchronous

所以的话:


  1. 我需要得到全球性的项目清单。

  2. 精细。项目获取。

  3. 请采取行动要求该用户已接管previously获取项目。

  4. 精细。操作牵强。

  5. 申请行动项目清单。

  6. 放在针对模型和显示项目。

这是有点的code我用

This is somewhat the code I'm using

return itemsResource
       .getItems(userId)
       .$promise
       .then(function(items) {
           return actionsResource
                  .getActions(items.map(i => i.id)) // pseudo mapper code here
                  .$promise
                  .then(function(actions) { // THIS IS NESTED so it sees both promise results
                      return [items, actions];
                  });
       })
       .then(helper.spread(function(items, actions) {
           applyActions(items, actions);
           $scope.model.items = items;
           return items;
       }));

你可以理解我不能使用 $ q.all 最初是因为第二(操作)承诺取决于第一个(项目)的结果。

As you can understand I can't use $q.all initially because second (actions) promise depends on results of the first one (items).

为什么不回用项目的行动?因为我缓存为所有用户的项目,因此项目取的真快。这类似于#2是如何工作的。他们只是返回的问题,无论用户请求他们。然后,他们随后还请求preferred而忽视标签,适用于牵强的问题。这个的非常好,否则,就要求更多的服务器来处理只是为所有用户问题初步名单,因为每个用户的请求将产生不同的结果,因此将高速缓存没有太大的意义。

Why not return actions with items? Because I'm caching items for all users, so item fetching is really quick. This is similar to how Stackoverflow works. They just return questions regardless of user requesting them. Then they subsequently also request for preferred and ignored tags and apply to fetched questions. This scales very well, otherwise SO would require many more servers to handle just initial list of questions for all users, because every user's request would yield different results, so caching wouldn't make much sense.

为什么是后不适项目 $范围牵强?这将消除额外的嵌套然后,这是事实。但我没有这样做,因为有几个步骤之后,每一次的承诺之一得到解决至少一个 $摘要正在执行周期。由于许多项目(是相当复杂的对象),这可以解释相当一些处理。这就是我坚持着通过项目视图到最后一刻可能的原因。

Why not apply items to $scope immediately after being fetched? This would eliminate the additional nested then, that's true. but I'm not doing that because there are few more steps afterwards and every time one of the promises gets resolved at least one $digest cycle is being executed. With many items (being rather complex objects) this can account for quite some processing. That's the reason I'm holding on to passing items to the view to the very last moment possible.

有没有办法避免嵌套的然后这两种解决方法旁:

Is there a way to avoid the nested then beside these two workarounds:


  1. 尽快
  2. 应用项目 $范围
  3. 项目保存到本地变量,之后用它

我想打平我的code尽可能不用介绍,如果可能的?更多的资源

I'd like to flatten my code as much as possible without introduction of additional resources if possible?

推荐答案

除非我失去了一些东西,这应该是相当简单的,不是吗?

Unless I'm missing something, this should be fairly straightforward, no?

(我已经简化了一些为清楚起见,您的内部函数签名)

(I have simplified some of your internal function signatures for clarity)

itemsResource.getItems(userId)
  .then(function(items) {
    return $q.all({
      items: items,
      actions: actionResource.getActions(items)
    });
  })
  .then(function(data) {
    applyActions(data.items, data.actions);
    $scope.model.items = data.items;
    return data.items;
  });

plunker 为了说明

这篇关于可这个承诺嵌套改为链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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