队列中的javascript函数

时间:2016-02-14 15:55:58

标签: javascript node.js

当我运行我的代码时,我得到以下内容,它发生得太快,似乎只是激发而不是等待异步回调。

4/4: Adding events for week 9 [###################] 12/12
1/4: Adding events for week 6 [###################] 20/20
2/4: Adding events for week 7 [###################] 32/32
3/4: Adding events for week 8 [###################] 40/40

或者如果我打印每一行而不是更新行

4/4: Adding events for week 9 [##########         ] 6/12
2/4: Adding events for week 7 [##                 ] 1/32
2/4: Adding events for week 7 [###                ] 2/32
2/4: Adding events for week 7 [#####              ] 3/32
1/4: Adding events for week 6 [###                ] 1/20
1/4: Adding events for week 6 [#####              ] 2/20
2/4: Adding events for week 7 [######             ] 4/32
2/4: Adding events for week 7 [########           ] 5/32
2/4: Adding events for week 7 [#########          ] 6/32
3/4: Adding events for week 8 [#                  ] 1/40
3/4: Adding events for week 8 [###                ] 2/40
3/4: Adding events for week 8 [####               ] 3/40
4/4: Adding events for week 9 [############       ] 7/12
4/4: Adding events for week 9 [#############      ] 8/12
4/4: Adding events for week 9 [#################  ] 9/12

它不应该发生,它应该首先添加第6周的所有事件,然后是7等等。

我希望在添加事件(异步)时运行队列中的下一个函数,基于此 https://stackoverflow.com/a/3584027/3774580队列管理器(稍微改了一下。)

var Queue = function(){
    this.queue = [];
    this.index = 0;
    this.cb = function(){};
};

Queue.prototype = {
    add: function(fn){
        this.queue.push({
            fn: fn
        });
    },
    run: function(index){
        (index || index === 0) && (this.index = index);
        this.next();
    },
    next: function(){
        var self = this,
            i = this.index++,
            at = this.queue[i],
            next = this.queue[this.index];

        if(at){ 
            at.fn();
        }
        if(next){
            self.next();
        }else if(this.index == this.queue.length){
            this.cb.call(this);
        }
    },
    reset: function(){
        this.index = 0;
    },
    onEnd: function(callback){
        this.cb = callback;
    }
};

在测试时一切正常,超时,然后我用队列替换超时,以最大限度地减少等待下一次迭代的浪费时间。 所以这很有效:

var timeout = 0;
weeks.forEach(function(week, index){
    setTimeout(function() {
        webuntis.fetchWeek(week, class_id, function(event){ /* ... */ });
    }, timeout );
    timeout += 3000;
});

这是我使用它的地方,尝试添加500ms超时作为测试

var WeekQueue = new Queue();
weeks.forEach(function (week, index) { //weeks = array with Date objects
    WeekQueue.add(function () {
        webuntis.fetchWeek(week, result.class_id, function (events) { //asyc callback
            var EventQueue = new Queue(), 
                bar = new ProgressBar((index + 1) + '/' + weeks.length + ': Adding events for week ' + week.getWeek(1) + ' [:bar] :current/:total', {/* config removed */});

            for (var e in events) {
                EventQueue.add(function () {
                    var event = events[e]; /* rest of vars removed */
                    icloud.addEvent(cal_ref, event.uid, start, end, description, event.zone[0], event.title[0], function (err, res) { //async callback
                        if (!err) {
                            bar.tick();
                            setTimeout(function () {
                                EventQueue.next()
                            }, 500);
                        }
                    });
                });
            }
            EventQueue.onEnd(function () {
                setTimeout(function () {
                    WeekQueue.next()
                }, 500);
            });
            EventQueue.run();
        });// /webuntis.fetchWeek
    });// /WeekQueue.add
});// /weeks.forEach
WeekQueue.run();

我做错了什么?

1 个答案:

答案 0 :(得分:0)

感谢@ShanShan它可行,最后使用https://github.com/caolan/async#queue而不是其他队列管理器