如果是这样的话:
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()
中,我怎样才能只返回那些已经解决的承诺?
答案 0 :(得分:1)
$q.all
仅用于解决all of the promises you pass it解决后的问题。比如,只有在所有4个小部件都已加载后才显示您的仪表板,这样做很有用。
如果您不想要这种行为,也就是说,您希望尝试显示可以成功解析的列数,那么您必须使用其他方法。
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