How to save a callback from an ajax in a variable

1

Good evening I am doing 2 requests ajax to 2 services which I managed to get the information in 2 functions as I do to unify the 2 functions since each one has a different data or how to save each of those functions in a variable thanks of beforehand.

function nombre(t,callback) {


            $.ajax({
                url: url1,
                method:'GET',
                type: 'JSON'
            }).done(function (alumnos) {

                var alum = JSON.parse(alumnos);

                var  nnn= alum[t-1].NOMBRE_USUARIO;

                callback(nnn);

                //console.log(nnn);


            });

        }


        function evento(obj,callback) {

            $.ajax({
                url: url,
                method:'GET',
                type: 'JSON'
            }).done(function (eventos) {

                var even = JSON.parse(eventos);

              var evento = even[obj-1].NOMBRE_EVENTO;

              callback(evento);

              //console.log(evento);

           });

        }

        function mycallback(dato1) {

            var dat1 = dato1;

            console.log(dat1);

        }


        function mycallback1(dato2) {

            var dat2 = dato2;

            console.log(dat2);

            return dato2;

        }


nombre(t,mycallback);
evento(t,mycallback1);
    
asked by William T 07.11.2017 в 04:50
source

1 answer

0

The problem as such is not due to the call of the callbacks, the error itself, is because you have badly structured the code and it is not possible to do so, besides that it is not correct:

Functional solutions:

1. nest the so-called ajax, like this:

function consultar(t) {
    $.ajax({
        url: url1,
        method: 'GET',
        type: 'JSON'
    }).done(function(alumnos) {
        var alum = JSON.parse(alumnos);
        var nnn = alum[t - 1].NOMBRE_USUARIO;
        $.ajax({
            url: url,
            method: 'GET',
            type: 'JSON'
        }).done(function(eventos) {
            var even = JSON.parse(eventos);
            var evento = even[obj - 1].NOMBRE_EVENTO;
            finalizar(nnn, evento)                  
        });
    });
}

function finalizar(nombre, evento) {
    console.log(nombre, evento);
}

2. use promises:

You must use promises (although the so-called ajax, are already promisificados), the idea is that you nest the functions function nombre(t,callback) and function evento(obj,callback) within a promise and thus be able to change the callback by a return . I recommend using Native Promises , Bluebird , Async or Q

You should keep in mind that your code is quite simple, so it is not convenient to use an API, it is better to opt for option 1, or the native promises.

Annex:

I'm doing tests with the flow of information similar to yours, but without ajax; and everything works perfect.

function nombre(t, callback) {    
  console.log(1,t)
  callback(t);       
}
function mycallback(dato1) {    
  console.log(2,dato1);
}
nombre("nombre", mycallback);

Simple example Callbacks

    
answered by 07.11.2017 / 05:49
source