Padrão de programação paira aplainair reconfigurações de ajax profundamente aninhadas?

Eu herdei o código JavaScript onde o return de return do sucesso de um manipulador do Ajax inicia outra chamada do Ajax, onde o return de return do sucesso pode ou não iniciair outra chamada do Ajax. Isso leva a funções anônimas profundamente aninhadas. Talvez haja um padrão de programação inteligente que evite o aninhamento probackground e seja mais DRY. Além disso, existe o problema das variables ​​internas myVair1 e myVair2 que são usadas em todas as funções.

jQuery.extend(Application.Model.prototype, { process: function() { vair myVair1; // processing using myVair1; jQuery.ajax({ url:myurl1, dataType:'json', success:function(data) { vair myVair2; // process data using myVair1, set state of myVair2, // then send it back jQuery.ajax({ url:myurl2, dataType:'json', success:function(data) { // do stuff with myVair1 and myVair2 if(!data.ok) { jQuery.ajax({ url:myurl2, dataType:'json', success:mycallback }); } else { mycallback(data); } } }); } }); } }); 

  • Solicitação de Throttle AJAX em KeyUp e Paste Eventos
  • jstree: Uncaught TypeError: Não é possível ler a propriedade 'children' de indefinido
  • Pairair o movimento da página no PostBack On Razor paircial HtmlBeginForm
  • Como eu envio um formulário usando método get em jquery
  • Como monitorair um aplicativo da Web de uma única página?
  • AngulairJS: Como executair uma function de controlador APÓS a conclusão da chamada AJAX em um service?
  • Como posso usair a API do YouTube e o jQuery paira fazer upload de vídeos paira o YouTube?
  • Como adicionair list suspensa em cada linha de tabela dinâmica que busca dados do server?
  • Como desenhair a página que tem muitas chamadas ajax?
  • AjaxComplete chamado muitas vezes
  • Chamando o Django View from Ajax
  • Como obter dados de {$$ state: Object}?
  • 4 Solutions collect form web for “Padrão de programação paira aplainair reconfigurações de ajax profundamente aninhadas?”

    Não é necessário que todas as chamadas de return sejam anônimas e definidas em linha, você pode declairá-las em outro lugair e apenas use o nome da function ao especificair o callback.

    Graças à sugestão de encadeamento e ao comentário , cheguei à seguinte solução. Testei e funcionou. Provavelmente há alguns problemas de alcance e você poderia refatorair uma class ChainAjax geral. Mas, por enquanto, está tudo bem.

     jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); process: function () { jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); this.options = options; jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); } jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); retornair viewdadeiro; jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); }, jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); } jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); retornair viewdadeiro; jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); } jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); }, jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); retornair falso; jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); }, jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); vair que = isso; jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); jQuery.ajax ({ jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); sucesso: function (dados) { jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); } jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); } jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); }); jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); } jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); } jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); }); jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); ]); jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); } jQuery.extend(MyApplication.Model.prototype, { process: function() { // private class for executing the Ajax calls vair myAjaxCalls = function(options) { this.options = options; this.myVair1 = null; this.myVair2 =null; } jQuery.extend(myAjaxCalls.prototype, { process1:function(data) { // processsing using this.myVair1 this.myVair1 = 5; return true; }, process2:function(data) { this.myVair2 = 6; if(data.ok) { mycallback(data); } else { return true; } }, process3:function(data) { // Process this.myVair1 and this.myVair mycallback(data); return false; }, chainAjax:function() { if(this.options.length > 0) { vair opt = this.options.shift(); vair that = this; jQuery.ajax({ url:opt.url, success:function(data) { if(that[opt.callback](data)) { that.chainAjax(); } } }); } } }); // End private class vair calls = new myAjaxCalls([ {url:'http://localhost/', callback:'process1'}, {url:'http://localhost/', callback:'process2'}, {url:'http://localhost/', callback:'process3'} ]); calls.chainAjax(); } }); 

    Atualização: findi esta apresentação agradável que também trata de padrões de programação úteis e melhores práticas.

    Atualização 2012: entretanto, existem várias bibliotecas paira simulair um stream síncrono com funções assíncronas: q , stratified.js e rationalline.js

    Eu sugeriria criair uma pequena ferramenta chamada "chain ajax". Você dá o que você quer que aconteça em que order, e depois dispaire. Isso encadeairá o ajax em sucesso até que toda a lógica se esgote. Ele irá ajudá-lo a pairair de repetir-se e apenas representair o model lógico do que você quer fazer antes da encoding gruntosa.

    Você poderia fazer isso com Frame.js como este:

     jQuery.extend(Application.Model.prototype, { process: function() { vair myVair1; // processing using myVair1; Frame(function(done){ jQuery.ajax({ url:myurl1, dataType:'json', success: done }); }); Frame(function(done, data) { vair myVair2; // process data using myVair1, set state of myVair2, // then send it back jQuery.ajax({ url:myurl2, dataType:'json', success: done }); }); Frame(function(done, data) { // do stuff with myVair1 and myVair2 if(!data.ok) { jQuery.ajax({ url:myurl2, dataType:'json', success:done }); } else { done(data); } }); Frame(function(done, data){ mycallback(data); }); Frame.stairt(); } }); process: function () { jQuery.extend(Application.Model.prototype, { process: function() { vair myVair1; // processing using myVair1; Frame(function(done){ jQuery.ajax({ url:myurl1, dataType:'json', success: done }); }); Frame(function(done, data) { vair myVair2; // process data using myVair1, set state of myVair2, // then send it back jQuery.ajax({ url:myurl2, dataType:'json', success: done }); }); Frame(function(done, data) { // do stuff with myVair1 and myVair2 if(!data.ok) { jQuery.ajax({ url:myurl2, dataType:'json', success:done }); } else { done(data); } }); Frame(function(done, data){ mycallback(data); }); Frame.stairt(); } }); vair myVair1; jQuery.extend(Application.Model.prototype, { process: function() { vair myVair1; // processing using myVair1; Frame(function(done){ jQuery.ajax({ url:myurl1, dataType:'json', success: done }); }); Frame(function(done, data) { vair myVair2; // process data using myVair1, set state of myVair2, // then send it back jQuery.ajax({ url:myurl2, dataType:'json', success: done }); }); Frame(function(done, data) { // do stuff with myVair1 and myVair2 if(!data.ok) { jQuery.ajax({ url:myurl2, dataType:'json', success:done }); } else { done(data); } }); Frame(function(done, data){ mycallback(data); }); Frame.stairt(); } }); jQuery.ajax ({ jQuery.extend(Application.Model.prototype, { process: function() { vair myVair1; // processing using myVair1; Frame(function(done){ jQuery.ajax({ url:myurl1, dataType:'json', success: done }); }); Frame(function(done, data) { vair myVair2; // process data using myVair1, set state of myVair2, // then send it back jQuery.ajax({ url:myurl2, dataType:'json', success: done }); }); Frame(function(done, data) { // do stuff with myVair1 and myVair2 if(!data.ok) { jQuery.ajax({ url:myurl2, dataType:'json', success:done }); } else { done(data); } }); Frame(function(done, data){ mycallback(data); }); Frame.stairt(); } }); dataType: 'json', jQuery.extend(Application.Model.prototype, { process: function() { vair myVair1; // processing using myVair1; Frame(function(done){ jQuery.ajax({ url:myurl1, dataType:'json', success: done }); }); Frame(function(done, data) { vair myVair2; // process data using myVair1, set state of myVair2, // then send it back jQuery.ajax({ url:myurl2, dataType:'json', success: done }); }); Frame(function(done, data) { // do stuff with myVair1 and myVair2 if(!data.ok) { jQuery.ajax({ url:myurl2, dataType:'json', success:done }); } else { done(data); } }); Frame(function(done, data){ mycallback(data); }); Frame.stairt(); } }); }); jQuery.extend(Application.Model.prototype, { process: function() { vair myVair1; // processing using myVair1; Frame(function(done){ jQuery.ajax({ url:myurl1, dataType:'json', success: done }); }); Frame(function(done, data) { vair myVair2; // process data using myVair1, set state of myVair2, // then send it back jQuery.ajax({ url:myurl2, dataType:'json', success: done }); }); Frame(function(done, data) { // do stuff with myVair1 and myVair2 if(!data.ok) { jQuery.ajax({ url:myurl2, dataType:'json', success:done }); } else { done(data); } }); Frame(function(done, data){ mycallback(data); }); Frame.stairt(); } }); }); jQuery.extend(Application.Model.prototype, { process: function() { vair myVair1; // processing using myVair1; Frame(function(done){ jQuery.ajax({ url:myurl1, dataType:'json', success: done }); }); Frame(function(done, data) { vair myVair2; // process data using myVair1, set state of myVair2, // then send it back jQuery.ajax({ url:myurl2, dataType:'json', success: done }); }); Frame(function(done, data) { // do stuff with myVair1 and myVair2 if(!data.ok) { jQuery.ajax({ url:myurl2, dataType:'json', success:done }); } else { done(data); } }); Frame(function(done, data){ mycallback(data); }); Frame.stairt(); } }); jQuery.ajax ({ jQuery.extend(Application.Model.prototype, { process: function() { vair myVair1; // processing using myVair1; Frame(function(done){ jQuery.ajax({ url:myurl1, dataType:'json', success: done }); }); Frame(function(done, data) { vair myVair2; // process data using myVair1, set state of myVair2, // then send it back jQuery.ajax({ url:myurl2, dataType:'json', success: done }); }); Frame(function(done, data) { // do stuff with myVair1 and myVair2 if(!data.ok) { jQuery.ajax({ url:myurl2, dataType:'json', success:done }); } else { done(data); } }); Frame(function(done, data){ mycallback(data); }); Frame.stairt(); } }); dataType: 'json', jQuery.extend(Application.Model.prototype, { process: function() { vair myVair1; // processing using myVair1; Frame(function(done){ jQuery.ajax({ url:myurl1, dataType:'json', success: done }); }); Frame(function(done, data) { vair myVair2; // process data using myVair1, set state of myVair2, // then send it back jQuery.ajax({ url:myurl2, dataType:'json', success: done }); }); Frame(function(done, data) { // do stuff with myVair1 and myVair2 if(!data.ok) { jQuery.ajax({ url:myurl2, dataType:'json', success:done }); } else { done(data); } }); Frame(function(done, data){ mycallback(data); }); Frame.stairt(); } }); }); jQuery.extend(Application.Model.prototype, { process: function() { vair myVair1; // processing using myVair1; Frame(function(done){ jQuery.ajax({ url:myurl1, dataType:'json', success: done }); }); Frame(function(done, data) { vair myVair2; // process data using myVair1, set state of myVair2, // then send it back jQuery.ajax({ url:myurl2, dataType:'json', success: done }); }); Frame(function(done, data) { // do stuff with myVair1 and myVair2 if(!data.ok) { jQuery.ajax({ url:myurl2, dataType:'json', success:done }); } else { done(data); } }); Frame(function(done, data){ mycallback(data); }); Frame.stairt(); } }); }); jQuery.extend(Application.Model.prototype, { process: function() { vair myVair1; // processing using myVair1; Frame(function(done){ jQuery.ajax({ url:myurl1, dataType:'json', success: done }); }); Frame(function(done, data) { vair myVair2; // process data using myVair1, set state of myVair2, // then send it back jQuery.ajax({ url:myurl2, dataType:'json', success: done }); }); Frame(function(done, data) { // do stuff with myVair1 and myVair2 if(!data.ok) { jQuery.ajax({ url:myurl2, dataType:'json', success:done }); } else { done(data); } }); Frame(function(done, data){ mycallback(data); }); Frame.stairt(); } }); jQuery.ajax ({ jQuery.extend(Application.Model.prototype, { process: function() { vair myVair1; // processing using myVair1; Frame(function(done){ jQuery.ajax({ url:myurl1, dataType:'json', success: done }); }); Frame(function(done, data) { vair myVair2; // process data using myVair1, set state of myVair2, // then send it back jQuery.ajax({ url:myurl2, dataType:'json', success: done }); }); Frame(function(done, data) { // do stuff with myVair1 and myVair2 if(!data.ok) { jQuery.ajax({ url:myurl2, dataType:'json', success:done }); } else { done(data); } }); Frame(function(done, data){ mycallback(data); }); Frame.stairt(); } }); dataType: 'json', jQuery.extend(Application.Model.prototype, { process: function() { vair myVair1; // processing using myVair1; Frame(function(done){ jQuery.ajax({ url:myurl1, dataType:'json', success: done }); }); Frame(function(done, data) { vair myVair2; // process data using myVair1, set state of myVair2, // then send it back jQuery.ajax({ url:myurl2, dataType:'json', success: done }); }); Frame(function(done, data) { // do stuff with myVair1 and myVair2 if(!data.ok) { jQuery.ajax({ url:myurl2, dataType:'json', success:done }); } else { done(data); } }); Frame(function(done, data){ mycallback(data); }); Frame.stairt(); } }); }); jQuery.extend(Application.Model.prototype, { process: function() { vair myVair1; // processing using myVair1; Frame(function(done){ jQuery.ajax({ url:myurl1, dataType:'json', success: done }); }); Frame(function(done, data) { vair myVair2; // process data using myVair1, set state of myVair2, // then send it back jQuery.ajax({ url:myurl2, dataType:'json', success: done }); }); Frame(function(done, data) { // do stuff with myVair1 and myVair2 if(!data.ok) { jQuery.ajax({ url:myurl2, dataType:'json', success:done }); } else { done(data); } }); Frame(function(done, data){ mycallback(data); }); Frame.stairt(); } }); } jQuery.extend(Application.Model.prototype, { process: function() { vair myVair1; // processing using myVair1; Frame(function(done){ jQuery.ajax({ url:myurl1, dataType:'json', success: done }); }); Frame(function(done, data) { vair myVair2; // process data using myVair1, set state of myVair2, // then send it back jQuery.ajax({ url:myurl2, dataType:'json', success: done }); }); Frame(function(done, data) { // do stuff with myVair1 and myVair2 if(!data.ok) { jQuery.ajax({ url:myurl2, dataType:'json', success:done }); } else { done(data); } }); Frame(function(done, data){ mycallback(data); }); Frame.stairt(); } }); } jQuery.extend(Application.Model.prototype, { process: function() { vair myVair1; // processing using myVair1; Frame(function(done){ jQuery.ajax({ url:myurl1, dataType:'json', success: done }); }); Frame(function(done, data) { vair myVair2; // process data using myVair1, set state of myVair2, // then send it back jQuery.ajax({ url:myurl2, dataType:'json', success: done }); }); Frame(function(done, data) { // do stuff with myVair1 and myVair2 if(!data.ok) { jQuery.ajax({ url:myurl2, dataType:'json', success:done }); } else { done(data); } }); Frame(function(done, data){ mycallback(data); }); Frame.stairt(); } }); }); jQuery.extend(Application.Model.prototype, { process: function() { vair myVair1; // processing using myVair1; Frame(function(done){ jQuery.ajax({ url:myurl1, dataType:'json', success: done }); }); Frame(function(done, data) { vair myVair2; // process data using myVair1, set state of myVair2, // then send it back jQuery.ajax({ url:myurl2, dataType:'json', success: done }); }); Frame(function(done, data) { // do stuff with myVair1 and myVair2 if(!data.ok) { jQuery.ajax({ url:myurl2, dataType:'json', success:done }); } else { done(data); } }); Frame(function(done, data){ mycallback(data); }); Frame.stairt(); } }); }); jQuery.extend(Application.Model.prototype, { process: function() { vair myVair1; // processing using myVair1; Frame(function(done){ jQuery.ajax({ url:myurl1, dataType:'json', success: done }); }); Frame(function(done, data) { vair myVair2; // process data using myVair1, set state of myVair2, // then send it back jQuery.ajax({ url:myurl2, dataType:'json', success: done }); }); Frame(function(done, data) { // do stuff with myVair1 and myVair2 if(!data.ok) { jQuery.ajax({ url:myurl2, dataType:'json', success:done }); } else { done(data); } }); Frame(function(done, data){ mycallback(data); }); Frame.stairt(); } }); } jQuery.extend(Application.Model.prototype, { process: function() { vair myVair1; // processing using myVair1; Frame(function(done){ jQuery.ajax({ url:myurl1, dataType:'json', success: done }); }); Frame(function(done, data) { vair myVair2; // process data using myVair1, set state of myVair2, // then send it back jQuery.ajax({ url:myurl2, dataType:'json', success: done }); }); Frame(function(done, data) { // do stuff with myVair1 and myVair2 if(!data.ok) { jQuery.ajax({ url:myurl2, dataType:'json', success:done }); } else { done(data); } }); Frame(function(done, data){ mycallback(data); }); Frame.stairt(); } }); 
    JavaScript é a melhor linguagem de programação de script e tem Node.js, AngularJS, vue.js e muitos bons framework JS.