Work with several requests to the server [Asynchrony] SetTimeOut, Promises ...?

0

I am working with several AJAX requests, some to PHP, others to txt ...

The problem comes when working with the data returned from these requests to form something together (like a table). How can I work with all the information at once?

Investigating a bit I found this article that I recommend your reading that basically gives me two solutions:

  • CallBacks
  • Promises
  • Callbacks seem like a sloppy and terribly inefficient solution, but the promises are really interesting but even trying out examples I do not know how to use them.

    I'll give you a quick example where I'm trying it:

    window.onload = function(){
     var global="nulisimo";
     var global_segundo="nulisimo_2";
    
     //Cargo el primer TXT 
     cargar_txt();
     var global = procesar_txt();
    
     //Cargo el segundo TXT
     cargar_txt_segundo();
     var global_segundo = procesar_txt_segundo();
    
     setTimeout(function(){
     console.log("Global primera :",global);
     console.log("Global segunda :",global_segundo);
     }, 2000);
    
    Promise.all([procesar_txt(), procesar_txt_segundo()])
    .then(resultArray => console.log("Valores globales :",global,"/",global_segundo))
    .catch(e => console.log('Error capturado:  ${e}'));
    
    } 
    

    Loading TXT is the AJAX request itself, and the only answer it contains is this:

    function procesar_txt() {
    if(peticion_http.readyState == 4) 
    {
        if(peticion_http.status == 200) 
        {
            var cadena = peticion_http.responseText;
            console.log("La cadena en el 1 es  :",cadena);
            return cadena;
    
    
        }
      }
    }
    

    I can not get back the values of the functions, neither by CallBacks nor by promise. Any idea or example of how to handle the results of several AJAX requests? PD: Is the correct way to treat answers with return?

    Thank you very much.

        
    asked by Roarca 31.03.2018 в 16:56
    source

    1 answer

    0

    From what I've seen in the code, I've noticed that the functions you pass to Promise.all([]) do not resolve any promises.

    The Promise.all([]) method receives an iterable of promises, with which each of your requests must return a promise, in this way the Promise.all([]) method will solve when each of the functions it receives are found.

    I show you a similar example.

    var procesar_txt = function() {
      return new Promise(function(resolve, reject) {
        setTimeout(function() {
          resolve('RESUELTA PRIMERA PETICIÓN');
        }, 2000);
      })
    }
    
    var procesar_txt_segundo = function() {
      return new Promise(function(resolve, reject) {
        setTimeout(function() {
          resolve('RESUELTA SEGUNDA PETICIÓN');
        }, 5000);
      })
    }
    
    //Cargo el primer TXT 
    
    Promise.all([procesar_txt(), procesar_txt_segundo()])
      .then((result) => {
        alert(result.join(','));
    
      })
      .catch(e => alert('Error capturado:  ${e}'));

    It's almost your own code, only that I've added a promise to each function.

        
    answered by 31.03.2018 / 18:42
    source