如何queue.js工作? [英] How does queue.js work?

查看:380
本文介绍了如何queue.js工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图了解如何迈克·博斯托克的queue.js 工作,但我看不出它如何管理工作。我不明白的部分是如何code设法继续执行回调。特别是,我不能确定有关 POP()方法(第45行)。从我的理解,该方法将在下次未处理,延迟功能;附加在队列中(潜在地)开始下一个延迟功能,当立即弹出功能完成执行的回调;那么最终执行表示的功能。我的问题是:什么code执行这个回调?

I've been trying to understand how Mike Bostock's queue.js works, but I can't see how it manages to work. The part I don't understand is how the code manages to continue executing callbacks. In particular, I am unsure about the pop() method (line 45). From my understanding, the method takes the next unprocessed, deferred function; appends a callback that (potentially) starts the next deferred function in the queue and executes when the immediately popped function finishes; then finally executes said function. My question is: what code executes this callback?

推荐答案

每个推迟功能实际上并不返回任何东西 - 他们预计执行其最后一个参数作为回调。例如,这是不行的。

Each deferred function does not actually return anything -- they are expected to execute their final argument as a callback. For example, this will not work

var foo = function(i) {
  console.log(i);
  return i;
}
var finished = function(error, results) {
  console.log(results);
}

queue(2)
  .defer(foo, 1)
  .defer(foo, 2)
  .defer(foo, 3)
  .defer(foo, 4)
  .awaitAll(finished);  // only prints "1" and "2", since foo() doesn't execute callbacks

不过,如果我们修改来采取一个回调,

var foo = function(i, callback) {
  console.log(i);
  callback(null, i);  // first argument is error reason, second is result
}

然后,它会,作为执行回调的原因队列继续。

这篇关于如何queue.js工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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