Using Promises and Deffered in daily life

2

I'm studying about these items and I'm not sure how to apply them in my projects. I currently use callback for everything but the code structure gets tense .. and on the internet I found a lot of articles talking about the Promises and Deffered .. but I could not understand how that would be in practice this ..

If you do not abuse, but already abusing, I would like to understand how the following situation would be, I have 3 functions and each one has a callback, so function 2 is only called when function 1 ends and the callback returns of your data, so function 2 executes and then calls function 3, and in this way successively for more functions if it has.

Then my code would look like this (this code is an example only)

function funcao1(callback){
    $.ajax({
                type: "POST",
                url: minhaUrlPost,
                data: {
                  acao: 'CarregaDadosCliente',
                  codigo: codigo
                },                            
                dataType: "json", 
                success: function (json){
                  return callback(json);
                },error: function (){
                  alert("deu erro")
                }
    });
}

function funcao2(cod_cliente,callback){
    $.ajax({
                type: "POST",
                url: minhaUrlPost,
                data: {
                  acao: 'CarregaPedidosCliente',
                  codigo: cod_cliente
                },                            
                dataType: "json", 
                success: function (json){
                  return callback(json);
                },error: function (){
                  alert("deu erro")
                }
    });
}

function funcao3(cod_pedido,callback){
    $.ajax({
                type: "POST",
                url: minhaUrlPost,
                data: {
                  acao: 'CarregaItensPedidoCliente',
                  codigo: cod_pedido
                },                            
                dataType: "json", 
                success: function (json){
                  return callback(json);
                },error: function (){
                  alert("deu erro")
                }
    });
}

funcao1(function(json){
    funcao2(json.cod_cliente, function(json){
        funcao3(json.cod_pedido, function(json){
            //faz alguma coisa ou chamaria mais functions...
        });
    });
});

The example above works ... but I know that it can improve both this structure and the working method used. and would like the help of the community to better understand this in the practice of the above example.

Thanks in advance !!!!

    
asked by anonymous 12.07.2016 / 14:51

1 answer

0

Hello. If the calls are very similar you can do something like this:

var url = '/echo/json/';
var acao = 'CarregaDadosCliente';
var dados = {acao: acao,codigo: null};

function funcao1(json,nom_cod){
    dados.codigo = typeof nom_cod === 'undefined' ? '' : json[nom_cod];

    $.post(url, dados, function(json){
        return json;
    }).fail(function() {
        alert("deu erro");
    });
}


alert(funcao1(funcao1(funcao1(),'cod_cliente'),'cod_pedido'));

You can even add url and action as a parameter if they are different. Follow link .

Another idea would be to create a function that gets an array of objects like:

[{url:'url1',acao:'acao1',cb:funcao2},{url:'url2',acao:'acao2',cb:funcao3}]

This function would be recursive and remove an item from the array at each end of the call.

I never needed to use Promises (just out of curiosity) I used this link link

In the "Error Handling" part you have a very good example of nested asynchronous calls. See if it helps.

    
02.08.2016 / 19:28