Javascript异步AJAX和递归排序
我的情况是,使用同步ajax请求闻起来像蛋糕 基本上,我需要弄清楚如何对异步请求排序,以便下一个请求等待,但我所需要的结构使这一点非常具有挑战性。我会尽力解释我所拥有的和我想要完成的Javascript异步AJAX和递归排序,javascript,jquery,ajax,asynchronous,recursion,Javascript,Jquery,Ajax,Asynchronous,Recursion,我的情况是,使用同步ajax请求闻起来像蛋糕 基本上,我需要弄清楚如何对异步请求排序,以便下一个请求等待,但我所需要的结构使这一点非常具有挑战性。我会尽力解释我所拥有的和我想要完成的 点击一个按钮 发送一个AJAX请求以获取需要完成的事情的列表 列表必须经过for循环,因此步骤是一个接一个的 现在是棘手的部分。有些步骤需要额外的ajax请求来获取如何继续的信息,但是在上一步没有完成的情况下,下一步无法继续 一个更棘手的部分是,一个步骤可能有一个完整的其他序列。还有一些步骤会向用户提供提示,必须等
var listf = ['print', 'ask', 'send', 'list', 'done'];
var lists = ['print2', 'ask2', 'done2'];
var promiseFor = (function(condition, action, value) {
var promise = new Promise(function(resolve, reject) {
if(!condition(value)) return;
return action(value).then(promiseFor.bind(null, condition, action));
});
return promise;
});
var initSequence = (function(n) {
var promise = new Promise(function(resolve, reject) {
if(n == 1) {
return runSequence(listf);
}
return runSequence(lists);
});
return promise;
});
var runSequence = (function(list) {
var promise = new Promise(function(resolve, reject) {
var count = 0;
promiseFor(function(count) {
return count < list.length;
}, function(count) {
return executeAction(list[count]).then(function(result) {
return ++count;
});
}, 0).then(console.log.bind(console, 'for done'));
});
});
var executeAction = (function(action) {
var promise = new Promise(function(resolve, reject) {
if(action == 'print') document.getElementById('output').innerHTML += 'Some text<br>';
if(action == 'print2') document.getElementById('output').innerHTML += 'Some text 2<br>';
if(action == 'ask') {
document.getElementById('output').innerHTML += 'Alert 1 for pause<br>';
alert('pausing');
}
if(action == 'ask2') {
document.getElementById('output').innerHTML += 'Alert 2 for pause<br>';
alert('pausing again');
}
if(action == 'send') {
setTimeout(function() {
document.getElementById('output').innerHTML += 'Text after delay<br>';
resolve(true);
}, 2000);
return;
}
if(action == 'list') {
document.getElementById('output').innerHTML += 'Starting subsequence<br>';
initSequence(2);
}
if(action == 'done') document.getElementById('output').innerHTML += 'Sequence done<br>';
if(action == 'done2') document.getElementById('output').innerHTML += 'Sequence 2 done<br>';
resolve(true);
});
return promise;
});
initSequence(1);
var listf=['print'、'ask'、'send'、'list'、'done'];
变量列表=['print2','ask2','done2'];
var promiseFor=(函数(条件、操作、值){
var承诺=新承诺(功能(解决、拒绝){
如果(!条件(值))返回;
返回操作(value).then(promiseFor.bind(null,condition,action));
});
回报承诺;
});
var initSequence=(函数(n){
var承诺=新承诺(功能(解决、拒绝){
如果(n==1){
返回运行序列(listf);
}
返回运行序列(列表);
});
回报承诺;
});
var runSequence=(函数(列表){
var承诺=新承诺(功能(解决、拒绝){
var计数=0;
承诺人(功能(计数){
返回计数';
if(action='print2')document.getElementById('output').innerHTML+='Some text 2
';
如果(操作==‘询问’){
document.getElementById('output')。innerHTML+=“暂停警报1
”;
警报(“暂停”);
}
如果(操作=='ask2'){
document.getElementById('output')。innerHTML+=“暂停警报2
”;
警报(“再次暂停”);
}
如果(操作==“发送”){
setTimeout(函数(){
document.getElementById('output')。innerHTML+='Text after delay
';
决心(正确);
}, 2000);
返回;
}
如果(操作==‘列表’){
document.getElementById('output')。innerHTML+='Starting subsequence
';
初始序列(2);
}
if(action='done')document.getElementById('output').innerHTML+='Sequence done
';
if(action=='done2')document.getElementById('output').innerHTML+='Sequence 2 done
';
决心(正确);
});
回报承诺;
});
初始序列(1);
您可能应该使用承诺将请求和执行链接在一起
var firstMethod = function() {
var promise = new Promise(function(resolve, reject){
setTimeout(function() {
console.log('first method completed');
resolve({data: '123'});
}, 2000);
});
return promise;
};
var secondMethod = function(someStuff) {
var promise = new Promise(function(resolve, reject){
setTimeout(function() {
console.log('second method completed');
resolve({newData: someStuff.data + ' some more data'});
}, 2000);
});
return promise;
};
var thirdMethod = function(someStuff) {
var promise = new Promise(function(resolve, reject){
setTimeout(function() {
console.log('third method completed');
resolve({result: someStuff.newData});
}, 3000);
});
return promise;
};
firstMethod()
.then(secondMethod)
.then(thirdMethod)
//... etc.
下面是一些可能有用的参考资料链接
同步ajax请求闻起来像蛋糕代码示例可以是纯javascript或jquery…..亲爱的,你在做什么?@Jai可能是错的,我的意思是,知道如何实现这一点的人可以用纯javascript或jquery语法编写一个示例。对这些请求进行排队的理想方法是什么?使用promise或类似于
$的不同对象。when()
?感谢您引导我走向正确的方向,我想我成功地完成了我需要做的事情。我已经编辑了我的问题,并添加了我提出的代码和一个指向codepen的链接,如果您有兴趣了解它是如何执行的。