3
I’m developing a Javascript application and need to queue for requests ajax, and I need one to be done after the other, because they manipulate some variables of my scope. The parameter async
of jQuery.ajax
is obsolete, how to make executions run in mode sync
?
I cannot post the code in full because it belongs to the company, but I can illustrate.
What do I have:
(function(){
var var10;
var var20;
var var30;
var var40;
function func10(){
return $.ajax({
url: '/path/to/file',
success: function(data){
/* Faça algo */
var10 = data.algumacoisa;
}
})
}
function func20(p){
return $.ajax({
url: '/path/to/file',
data: {'algumacoisa': p},
success: function(data){
/* Faça algo */
var20 = data.algumacoisa;
}
})
}
function func30(p){
return $.ajax({
url: '/path/to/file',
data: {'algumacoisa': p},
success: function(data){
/* Faça algo */
var30 = data.algumacoisa;
}
})
}
function func40(p){
return $.ajax({
url: '/path/to/file',
data: {'algumacoisa': p},
success: function(data){
/* Faça algo */
var40 = data.algumacoisa;
}
})
}
})();
What I want:
I need all functions to be carried out in order func10().func20(var10).func30(var20).func40(var30)
and also to respect success
, namely after each success
execute the next function. I’m not sure if the method $.when
does this because the functions cannot be performed parallel.
What I don’t want:
I don’t want to have to call every function on success
of the previous:
function func2(p){
return $.ajax({
url: '/path/to/file',
data: {'algumacoisa': p},
success: function(data){
/* Faça algo */
var20 = data.algumacoisa;
func30(var20);
}
})
}
'Cause if I need to put another call in the middle of the line, I don’t want to go out digging through the code and have to keep changing all the flames, I just want to move the line, for example:
func10().func15(var10).func20(var15).func30(var20).func40(var30)
Is it possible to do this? If so, what is the best way? If not, why?
It was worth the thought, but it doesn’t work with requests
jQuery.ajax
, as they return an object before the completion of the execution. That is, the instructionif(funcaoa)
would return true and would begin to run thefuncaob
, however thefuncaoa
would still be running.– KaduAmaral
'Cause it’s buddy, here it comes What I don’t want of the question, why will I have to place the call in callback’s of each request.
– KaduAmaral
or Voce can call the first function by initiating an object like :
status = function()...
nextstatus.done(function () {
– Michel Simões
Edit your answer putting this example functionally. : D
– KaduAmaral
try it now...
– Michel Simões