循环调用一个异步函数

编程入门 行业动态 更新时间:2024-10-25 12:20:42
本文介绍了循环调用一个异步函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 问题简介

我需要在循环中调用一个异步函数,直到条件满足。这个特定的函数向一个网站 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的扩展中,这看起来像:

builder.addons.mozilla/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:

builder.addons.mozilla/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.

更多推荐

循环调用一个异步函数

本文发布于:2023-11-23 10:32:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621118.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!