0

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?

iLemming
  • 34,477
  • 60
  • 195
  • 309
  • 3
    That is expected ($q success will execute only if all the promises are `resolved`) you could resolve with no value in case of error. So you will get corresponding value in the array as undefined. – PSL Aug 13 '14 at 22:27
  • related: [How to detect when every promise has been executed](http://stackoverflow.com/q/19177087/1048572). – Bergi Aug 13 '14 at 22:31
  • 1
    Are you using [`Q`](http://stackoverflow.com/tags/q/info), or Angular's [`$q`](http://stackoverflow.com/questions/tagged/angular-promise)? Please choose the tags appropriately. – Bergi Aug 13 '14 at 22:31
  • well, I guess I can easily inject `Q` instead of angular's if it comes handy in this situation – iLemming Aug 14 '14 at 00:48
  • Actually you can't inject Q and use it since it doesn't expose setting the scheduler, you can inject Bluebird, use it with Angular and use `.settle` – Benjamin Gruenbaum Aug 14 '14 at 07:47
  • 1
    [Here](http://stackoverflow.com/q/23317555/1348195) this is a pretty close duplicate with $q, is that satisfactory? – Benjamin Gruenbaum Aug 14 '14 at 07:48
  • Benj what do you mean I can't? Already doing that `app.factory "$q",-> require "q"` Although I'm also using browserify, I don't know if that matters – iLemming Aug 14 '14 at 18:27

1 Answers1

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
SimplGy
  • 20,079
  • 15
  • 107
  • 144