简体   繁体   English

queue.js如何工作?

[英]How does queue.js work?

I've been trying to understand how Mike Bostock's queue.js works, but I can't see how it manages to work. 我一直试图理解Mike Bostock的queue.js是如何工作的,但我看不出它是如何运作的。 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). 特别是,我不确定pop()方法(第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

However, if we modify foo to take a callback, 但是,如果我们修改foo进行回调,

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

Then it will, as executing the callback causes queue to continue. 然后它将执行回调导致queue继续。

如果我正确理解代码, queue.await()queue.awaitall()将回调放在await实例变量中,然后由notify()执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM