使用Javascript:临睡前第三个功能测试两个非同步电话? [英] Javascript : testing two asynch calls before going to third function?

查看:105
本文介绍了使用Javascript:临睡前第三个功能测试两个非同步电话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有两个非同步调用走出去web服务......他们都需要为我的第三个方法。

So I have two asynch calls going out to a webservice... both of them are needed for my third method.

如何将你们建议这样做呢?

How would you guys suggest doing this?

我试着在我的方法之一,设置标志(其它方法调用第三种方法,在某些数据发送)...所以在我的第三个方法我试过这样:

I tried setting a flag in one of my methods (the other method calls the third method and send in some data) ... so in my third method I tried this:

 thirdMethod: function (returnedData) {

        if (this.secondFunctionReturned != true) {
            setTimeout(this.thirdMethod, returnedData, 100);
        }
        else {}

但这种超时没有工作(它没有通过数据对我来说) - 只是想知道你们的想法......感谢

But this timeout did not work (it didnt pass the data for me) -- just wondering what you guys think ... and thanks.

推荐答案

presumably你得到的数据从每个异步调用的后面,这就是为什么你烧你的第三个前需要他们。我不得不每个异步回调的检查,看看是否所有需要的数据是present,如果是这样,火了你的第三个功能。或者你可以使用一个计数器,但同样,presumably您的数据已经和计数器也只是不必要的额外的。

Presumably you're getting data back from each of the async calls and that's why you need them before firing your third. I'd have each of the async callbacks check to see if all required data is present and, if so, fire off your third function. Or you could use a counter, but again, presumably you have data already and the counter would just be unnecessarily additional.

伪:

var dataFromCall1, dataFromCall2;

startAsync(function(data) {
    dataFromCall1 = data;
    if (dataFromCall2) {
        thirdFunction(dataFromCall1, dataFromCall2);
    }
});

startOtherAsync(function(data) {
    dataFromCall2 = data;
    if (dataFromCall1) {
        thirdFunction(dataFromCall1, dataFromCall2);
    }
});

或者少加上:

var dataFromCall1, dataFromCall2;

startAsync(function(data) {
    dataFromCall1 = data;
    nextStep();
});

startOtherAsync(function(data) {
    dataFromCall2 = data;
    nextStep();
});

function nextStep() {
    if (dataFromCall1 && dataFromCall2) {
        thirdFunction(dataFromCall1, dataFromCall2);
    }
}

或者计数器:

var counter = 0;

startAsync(function() {
    // ...presumably some processing here...

    // Possibly fire next step
    handleCompletion();
});

startOtherAsync(function() {
    // ...presumably some processing here...

    // Possibly fire next step
    handleCompletion();
});

function handleCompletion() {
    if (++counter === 2) {
        thirdFunction();
    }
}


更新

对于在下面的意见问题:

Regarding the question in the comments below:

我想这一次,然后想到了一个实例,这两个回报率在同一时间...不能有这样的情况,如果他们都在准确的同一时间返回此会失败?

I tried this once then thought about the one instance that both of these return at the same time... couldn't there be a situation where this would fail if they both returned at the exact same time?

好问题,我应该已经解决了最初:没有,有没有同时出现两个调用的可能性。在JavaScript的Web浏览器是单线程的(除非明确使用网络工作者的的,即使如此,线程之间的相互作用是显性的)。如果第一次调用回调正在进行第二次调用完成后,第二次回调将排队,只运行时的第一个回调的回报。

Good question, I should have addressed that originally: No, there is no possibility of both calls occurring simultaneously. JavaScript in web browsers is single-threaded (barring the explicit use of web workers, and even then interactions between the threads are explicit). If the callback for the first call is in progress when the second call completes, the second callback will be queued and only run when the first callback returns.

这篇关于使用Javascript:临睡前第三个功能测试两个非同步电话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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