$q.all - take only those that resolved

感情迁移 提交于 2019-12-25 03:24:31

问题


in case like that:

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

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


回答1:


$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


来源:https://stackoverflow.com/questions/25296960/q-all-take-only-those-that-resolved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!