queue.js:功能与放大器阵列之间的差异;封阵? [英] queue.js: difference between array of functions & array of closures?

查看:143
本文介绍了queue.js:功能与放大器阵列之间的差异;封阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我想确保一系列HTTP GET请求的发生之前我尝试呈现陷入一个可视化的数据。典型的交易,对吧?

我使用queue.js,看到这个queue.js GitHub的页面上( https://开头的github .COM / mbostock /队列):


  

或者,如果你想(重新psented闭包的数组$ P $这里)连续运行bazillion异步任务:


 变种Q =队列(1);
    tasks.forEach(功能(T){q.defer(T);});
    q.awaitAll(功能(错误,结果){执行console.log(全部完成!);});


  

Queue.js可以在室内的Node.js或在浏览器中运行。


所以,我所做的就是制作功能,每个包含一个Meteor.http.get调用数组(因为我使用Meteor.js),然后跟着这个一行一行。

这似乎是正在发生的事情是,当我的阵列 - 这中有8个功能,所有的看起来像每个插槽右边的功能 - 被填充(然后作为在code exceprt通过推迟,)只有一个真正运行。

下面是什么我不知道。嗯,总的来说这是,为什么只有一种功能运行8被传递推迟?但具体是它的 - 有封闭的朦胧认识,我真的有一个函数数组。有什么我错过了那里,因为文件明确说倒闭,这就是为什么所有的功能都没有执行?

谢谢你看这个!


解决方案

下面也许就是你所引用的语句,在测试套件中的文本部分:

 的异步关闭,串行处理队列:{
    主题:功能(){
        变种任务= [],
            任务= asynchronousTask(),
            N = 10,
            Q =队列(1);        而(--n> = 0)tasks.push(任务);        tasks.forEach(功能(T){q.defer(T);});        q.awaitAll(this.callback)
    },
    不失败:功能(错误,结果){
        assert.isNull(错误);
    },
    执行串联所有任务:功能(错误,结果){
        assert.deepEqual(结果,[1,1,1,1,1,1,1,1,1,1]);
    }
},

https://github.com/mbostock/队列/ BLOB /主/测试/队列test.js#L103

其中运行任务= asynchronousTask(),这是关即拔的队列和调用

 函数asynchronousTask(){
    VAR活跃= 0;    返回函数(回调){
        ++活跃;        process.nextTick(函数(){
            尝试{
                回调(NULL,活动);
            } {最后
               - 活性;
            }
        });
    };
}

以上,内复位功能(){...} 是什么,我相信是被引用的关闭的,它保留它的引用的范围的外有效变量作为每个异步函数被调用了队列中。

这是当然,在回调中和处理程序方面相当强大,因为它可以让你保持和操纵本地共享变量,例如,如果你想知道有多少功能被退回的手段,当列表已经用完。换句话说,队列

下面的例子往上顶不使用,而是使用它作为一个参照点,看看它从上面的 synchronousTask 函数的不同之处。

 函数synchronousTask(){
    VAR活跃= 0;    返回函数(回调){
        尝试{
            回调(NULL,积极++);
        } {最后
             - 活性;
        }
    };
}

https://github.com/mbostock/队列/ BLOB /主/测试/队列test.js#L265

So, I am trying to make sure that a series of HTTP GET requests happen before I try to render the data gotten into a visualization. Typical deal, right?

I'm using queue.js, and seeing this on the queue.js github page (https://github.com/mbostock/queue):

Or, if you wanted to run a bazillion asynchronous tasks (here represented as an array of closures) serially:

    var q = queue(1);
    tasks.forEach(function(t) { q.defer(t); });
    q.awaitAll(function(error, results) { console.log("all done!"); });

Queue.js can be run inside Node.js or in a browser.

So, what I did was made an array of functions, each of which contained a Meteor.http.get call (as I'm using Meteor.js) and then followed this line by line.

It seems like what is happening is that while my array -- which has 8 functions in it, all with what looks like the right function in each slot -- gets populated (and then passed as in the code exceprt to defer,) only one actually runs.

Here's what I'm wondering. Well, overall it's, why is only one function running though 8 are passed in to defer? But specifically it's -- having a hazy understanding of closures, I really have an array of functions. Is there something I missed there, since the documentation specifically says closures, which is why all the functions aren't executing?

Thank you for looking at this!

解决方案

Here is perhaps the literal part of the statement you quoted, found in the test suite:

"queue of asynchronous closures, processed serially": {
    topic: function() {
        var tasks = [], 
            task = asynchronousTask(),
            n = 10,
            q = queue(1);

        while (--n >= 0) tasks.push(task);

        tasks.forEach(function(t) { q.defer(t); });

        q.awaitAll(this.callback)
    },
    "does not fail": function(error, results) {
        assert.isNull(error);
    },
    "executes all tasks in series": function(error, results) {
        assert.deepEqual(results, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]);
    }
},

https://github.com/mbostock/queue/blob/master/test/queue-test.js#L103

Which runs task = asynchronousTask(), which is what is pulled off the queue and invoked

function asynchronousTask() {
    var active = 0;

    return function(callback) {
        ++active;

        process.nextTick(function() {
            try {
                callback(null, active);
            } finally {
              --active;
            }
        });
    };
}

The above, inner return function() {...} is what I believe is being referenced as a closure which retains it's reference in scope to the outer active variable as each asynchronous function is called off the queue.

This is, of course, fairly powerful in terms of callbacks and handlers, since it gives you the means to maintain and manipulate a locally shared variable, for instance if you want to know how many functions were returned, and when the list has been exhausted. In other words, a queue.

The following is not used in the example up top, but use it as a reference point to see how it differs from the synchronousTask function above.

function synchronousTask() {
    var active = 0;

    return function(callback) {
        try {
            callback(null, ++active);
        } finally {
            --active;
        }
    };
}

https://github.com/mbostock/queue/blob/master/test/queue-test.js#L265

这篇关于queue.js:功能与放大器阵列之间的差异;封阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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