Q.js进度处理程序似乎没有触发 [英] Q.js progress handler doesn't seem to be firing

查看:59
本文介绍了Q.js进度处理程序似乎没有触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用Q.js进行承诺,因为它们实现了进度处理程序。但是,似乎他们没有开枪。我究竟做错了什么?看来很基本,所以我一定会缺少一些东西。 (下面的示例用coffeescript编写)

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 be missing something. (The example below is written in coffeescript)

Q = require('q')
squares = (list) ->
  deferred = Q.defer()
  result = list.map (e) ->
    r = e * e
    deferred.notify(r)
    return r
  deferred.resolve(result)
  return deferred.promise

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


推荐答案

此讨论线程位于github上: https://github.com。 OP和软件包维护者之间的com / kriskowal / q / issues / 188 提供了详细的说明和解决方案。

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.

总而言之,侦听器必须通过在承诺者调用 .notify 之前,先调用 .progress 。这意味着在 .progress 侦听器注册之前,承诺者正在同步调用通知的示例很可能会发生,因此该示例将显得不完整。 @bennedich提供的addTimeout解决方案满足此要求。

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.

此排序要求与通过调用注册的侦听器的体验不一致。然后 之后承诺者调用 .resolve 。在这种情况下,尽管有顺序,但仍会调用侦听器。但是要使该监听器被调用,Q程序包必须执行 magic (请参见向导帽子中的家伙: https://github.com/kriskowal )。故意不为收听者 .progress 提供相同的魔术,因此,此答案和先前答案中提到的排序要求。

This ordering requirement is not consistent with the experience of a listener registered via a call to .then after 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

这篇关于Q.js进度处理程序似乎没有触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆