Skip to content Skip to sidebar Skip to footer

Q.js Progress Handler Doesn't Seem To Be Firing

I'm currently using Q.js for promises, since they implement progress handlers. However, it doesn't seem like they're firing. What am I doing wrong? It seems pretty basic, so I must

Solution 1:

squares is executed synchronously, thus deferred.notify and deferred.resolve are invoked before any listeners are bound. Seems like already fulfilled promises still invokes the fulfilled callbacks but not any progress callbacks. You could wrap part of the code with setTimeout like this:

Q = require('q')

squares = (list) ->
  deferred = Q.defer()
  setTimeout (->
    result = list.map (e) ->
      r = e * e
      deferred.notify(r)
      return r
    deferred.resolve(result)
    ), 0return deferred.promise

squares([1,2,3,4,5,6,7,8,9,10])
  .progress((e) ->
    console.log e
  ).then((result) ->
    console.log result
  )

Solution 2:

This discussion thread on github : https://github.com/kriskowal/q/issues/188 between the OP and the package maintainer provides a detailed explanation and solution.

In summary, listeners must be registered via a call to .progress before the promiser makes calls to .notify. This means examples where the promiser is calling notify synchronously will likely occur before the .progress listener is registered, and thus the example will appear broken. The addTimeout solution provided by @bennedich satisfies this requirement.

This ordering requirement is not consistent with the experience of a listener registered via a call to .thenafter the promiser calls .resolve. In this scenario the listener is still called despite the ordering. But for that listener to be called after, the Q package has to perform magic (see the guy in the wizard hat : https://github.com/kriskowal). The same magic is deliberately NOT provided for listeners to .progress thus the ordering requirement mentioned in this and previous answers.

HTH

Post a Comment for "Q.js Progress Handler Doesn't Seem To Be Firing"