How do I get the value that is returning from the console.log(Results[4].geocode) to use in my if comparison?

Asked

Viewed 35 times

1

   var init = [iAppLib.getPersonalBits(),
               iAppLib.getGeoCode()                   
               ];
   var codes = false;
    var gcOk = new Array("BRH04700","BRH04600","BRH00200","BRH04500","BRJ01400","BRM00100","BRM00200","BRM09600","BRM04200");


    for(var i = 0; i < gcOk.length; i++){
        if(gcOk[i] == stb){
            codes= true;
        }
    }


  if(codes){
        document.write("</head>");
        document.write("<body background='bg.mpg'>");
        document.write("</body>");
        document.write("</html>");

    }else{
        document.write("</head>");
        document.write("<body background='tv:'>");
        document.write("</body>");
        document.write("</html>");
    };

   Promise.all(init).then(function (results) {
       console.log(results[4].geocode); 

   });

})();

1 answer

1


If the if you want to create is what is already in the code you posted, just put all the code (with the exception of the init function) inside the then() of your Promise. This will cause the code to run as soon as your promise results from the init function.

If if it is some other one that will go in a later implementation, just use the normal assignment:

Promise.all(init).then(function (results) {
   var meu_resultado = results[4].geocode;
   if(meu_resultado == minha_condicao){
       //Código do seu if
   }
});
  • Wow, it worked perfectly , I’m learning to program now , and I was not understanding this issue of promisse , even reading in forums and etc ....

  • The function of the promise is exactly to make asynchronous calls with its main code. This way, it executes the call and returns a "promise" that once executed, it will return you with the value. Then the code that is internal to then() will only be executed at the time the promise is fulfilled.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.