English 中文(简体)
专栏
原标题:Underscore js times series

I m 采用斜线,需要 方法。

然而,正如预期的那样,我需要以一系列方式开展这项工作,而不是像现在那样做。

是否有办法在一系列轮 to/回击方法中使用这种说法?

最佳回答

Given something like this:

function f() {
    some_async_call({ callback: function(err, results) {...})
}
_(3).times(f);

然后,三个<代码>f 电话将按系列进行,但<代码>sint_async_ calls 电话必定是连续发生的,因为它们是同步的。

如果你要迫使你发出一系列呼吁,那么你就应该利用“yn”呼吁,在系列中发出下一个呼吁:

function f(times, step) {
    step = step || 0;
    some_async_call({
        callback: function(err, results) {
            // Do something with `err` and `results`...
            if(step < times)
                f(times, step + 1);
        }
    });
}
f(3);

That approach will execute the three some_async_calls in series but, alas, the initial f(3) will return immediately. One solution to that problem is, of course, another callback:

function f(from_n, upto, and_finally) {
    some_async_call({
        callback: function(err, results) {
            // Do something with `err` and `results`...
            if(from_n < upto)
                f(from_n + 1, upto, and_finally);
            else
                and_finally();
        }
    });
}
f(0, 3, function() { console.log( all done ) });

<代码>在哪里? : is only a ∗∗∗∗

_.times = function(n, iterator, context) {
  for (var i = 0; i < n; i++) iterator.call(context, i);
};

。 如果你真心想要的话,你也许会错失,但你会做一个大的 me子,而不是简化你的法典。

您可以使用250R s async想法,但您必须建立一系列三个功能,但。 这比<代码>更合适。

// Untested off the top of my head code...

function f(callback) {
    some_async_call({
        callback: function(err, results) {
            // Deal with `err` and `results`...
            callback();
        }
    });
}

var three_fs = _(3).range().map(function() { return f });
async.series(three_fs);

但您仍需修改<条码>f,以备有退约功能,如果您总是重打<条码>f。 其后三次:

async.series([f, f, f]);

可能比用<代码>————>积极构建阵列更好。

这里的真正教训是,一旦你履行不尽人意的职能,你就会最终执行你的所有逻辑,作为呼吁退步的退步, all倒一切。

问题回答

This async library might get you started https://github.com/caolan/async#series

或者,如果你想这样做,想法是在每个功能反馈之后,在源代码





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.