jQuery.ajax async parameter is deprecated, now what?

3

I'm developing a Javascript application and need to queue ajax requests, and I need one to be done after the other because they manipulate some variables in my scope. The async parameter of the jQuery.ajax is deprecated, how do the executions run in sync mode?

I can not post the code fully because it belongs to the company, but I can give an example.

What 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 executed in order func10().func20(var10).func30(var20).func40(var30) and also that the success is respected, ie after each success execute the next function. I'm not sure if the $.when method does this because the functions can not be executed in parallel.

What I do not want:

I do not want to have to call each function in success of the previous one:

function func2(p){
    return $.ajax({
        url: '/path/to/file',
        data: {'algumacoisa': p},
        success: function(data){
            /* Faça algo */
            var20 = data.algumacoisa;
            func30(var20);
        }
    })
}

If you need to put another call in the middle of the queue, I do not want to go through the code and have to change all the flames, I just want to move the queue, 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?

    
asked by anonymous 30.04.2015 / 19:21

2 answers

1

You can have an array with the list of functions you want to run and run / empty the list as a callback.

The idea would be:

var funcArray = [function (data) {
    /* Faça algo */
    return data.algumacoisa;
}, function (data) {
    /* Faça algo */
    return data.algumacoisa;

}, function (data) {
    /* Faça algo */
    return data.algumacoisa;

}, function (data) {
    /* Faça algo */
    return data.algumacoisa;
}];

function tratadorAJAX(dataAnterior, next){
    if (!next) return;
    $.ajax({
        url: '/path/to/file',
        data: {'dataAnterior': dataAnterior},
        success: function(data){
            var proximaData = next(data);
            tratadorAJAX(proximaData, funcArray.pop());
        }
    })
}
tratadorAJAX(dataInicial, funcArray.pop());
    
01.05.2015 / 20:59
-2
status = (function(){ 
    funcaoMain();
 });

verificando = status.done(function () {
    funcaoOptional();
});
concluindo = verificando.done(function () {
    funcaoterceira();
});
    
30.04.2015 / 19:44