循环调用一个异步函数 [英] Loop calling an asynchronous function

查看:181
本文介绍了循环调用一个异步函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题简介

我需要在循环中调用一个异步函数,直到条件满足。这个特定的函数向一个网站 form.php 发送一个POST请求,并对响应进行一些操作,该响应是一个JSON字符串,表示具有 id 字段的对象。所以,当这个id为null时,外层循环必须结束。该函数执行如下所示的操作:
$ b

  function asyncFunction(session){
新的请求({
url:form.php,
content:sess =+ session,
onComplete:function(response){
var response = response.json;
if(response.id){
doStaff(response.msg);
} else {
// Break break
}
}
}))。得到();

注意:虽然我发现问题实现Firefox的附加组件,我认为这是一个通用的javascript问题。




实现循环递归地



我试图通过递归实现循环,但它没有工作,我不知道这是正确的方式。如果(response.id){
doStaff(response)

 的.msg); 
asyncFunction(session);
} else {
//断开循环
}
...

使用jsdeferred



我也试过用jsdeferred库:

  Deferred.define(this); 
//实例化一个新的延迟对象
var deferred = new Deferred();

//主循环:当我们收到异常时停止
Deferred.loop(1000,function(){
asyncFunction(session,deferred);
return deferred ;
})。
error(function(){
console.log(Loop finished!);
});

然后调用:

  ... 
if(response.id){
doStaff(response.msg);
d.call();
} else {
d.fail();

...

我实现了序列化,要求每一次迭代。例如,如果它是第三次调用asyncFunction,它将在迭代1和2中调用与相应参数相同的函数。

解决方案

为什么你不使用setInterval循环?在基于SDK的扩展中,这看起来像:

https://builder.addons.mozilla.org/addon/1065247/latest/



使用定时器的Promise-like模式就是你可以并行执行任务,并为各种任务使用更复杂的依赖关系。这样一个简单的循环就像使用setInterval一样简单/整齐地完成。


Introduction to the problem

I need to call an asynchronous function within a loop until a condition is satisfied. This particular function sends a POST request to a website form.php and performs some operations with the response, which is a JSON string representing an object with an id field. So, when that id is null, the outer loop must conclude. The function does something like the following:

function asyncFunction(session) {
    (new Request({
        url: form.php,
        content: "sess=" + session,
        onComplete: function (response) {
            var response = response.json;
            if (response.id) {
                doStaff(response.msg);
            } else {
                // Break loop
            }
        }
    })).get();
}   

Note: Although I've found the problem implementing an add-on for Firefox, I think that this is a general javascript question.


Implementing the loop recursively

I've tried implementing the loop by recursivity but it didn't work and I'm not sure that this is the right way.

...
    if (response.id) {
        doStaff(response.msg);
        asyncFunction(session);
    } else {
        // Break loop
    }
...

Using jsdeferred

I also have tried with the jsdeferred library:

 Deferred.define(this);
 //Instantiate a new deferred object
 var deferred = new Deferred(); 

 // Main loop: stops when we receive the exception
 Deferred.loop(1000, function() {
      asyncFunction(session, deferred);
      return deferred;
 }).
 error(function() {
     console.log("Loop finished!");
 });

And then calling:

...
    if (response.id) {
        doStaff(response.msg);
        d.call();
    } else {
        d.fail();
    }
...

And I achieve serialization but it started repeating previous calls for every iteration. For example, if it was the third time that it called the asyncFunction, it would call the same function with the corresponding parameters in the iterations 1 and 2.

解决方案

Why wouldn't you just use a setInterval loop? In the case of an SDK-based extension, this would look like:

https://builder.addons.mozilla.org/addon/1065247/latest/

The big benefit of promises-like patterns over using timers is that you can do things in parallel, and use much more complicated dependencies for various tasks. A simple loop like this is done just as easily / neatly using setInterval.

这篇关于循环调用一个异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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