$ q.all - 只需要那些解决 [英] $q.all - take only those that resolved

查看:241
本文介绍了$ q.all - 只需要那些解决的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在的情况下这样的:

getCol = (colId)->
   dfrd = $q.defer()
   if colId == "bacon"
       dfrd.reject()
   else
       dfrd.resolve colId
   dfrd.promise


getCols = (columns)->
   $q.all(_.map(columns, (cs)-> getCol(cs)))


getCols(['eggs','juice']).then (cols)->
   console.log cols                    # works



getCols(['eggs','juice','bacon']).then (cols)->
   console.log cols                  # not even getting here

所以,在 getCols()我怎么能只返回一个已经解决的承诺?

So, in getCols() how can I return only those promises that's been resolved?

推荐答案

$ q.all 意味着只有当解决的所有都解决了,你传递给它的承诺。这是很好的,说,只有一次显示全部4个部件装入您的仪表板。

$q.all is meant only to resolve when all of the promises you pass it have been resolved. It's good for, say, Only displaying your dashboard once all 4 widgets have loaded.

如果你不希望这样的行为,也就是说,你想尽量展现尽可能多列,能成功解决,你将不得不使用不同的方法。

If you do not want that behavior, that is, you'd like to try to show as many columns as could successfully be resolved, You'll have to use a different method.

loadedColumns = []

getCols = (columns) ->
  for col in columns
    willAdd = addColumn(col) # add column needs to store columns in the "loadedColumns" area, then resolve
    willAdd.then buildUI
    willAdd.catch logError


# Because this method is debounced, it'll fire the first time there is 50 ms of idleness
buildUI = _.debounce ->
  // Construct your UI out of "loadedColumns"
, 50

这篇关于$ q.all - 只需要那些解决的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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