问题
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