Wait variable is set to "open signal"

0

I have the variable x that passes through processes to be really defined and this takes a while, and to tinker with the rest of the application I need it to be defined, ie I need to wait for it to be defined if not the project crash ..

How to expect the variable to be set to release the other things .. I thought of an infinite setinterval but it would be gambiarra, is there something more robust?

    
asked by anonymous 01.07.2015 / 19:59

2 answers

0
// quero que perceba que é só um exemplo. Adapte para o seu código

var variavel = 0;
setaValorEmX(variavel, function(erro, resultado){

    //verifica se retornou erro da funcao assincrona
    if(erro){
        throw erro;
    }

    if (resultado) {
        console.log(resultado)
        //faz o seu codigo
    };


});


function setaValorEmX(x, callback){
    //coloque o codigo que seta o valor em x
    x = 1;

    //
    callback(null, x);
}
    
01.07.2015 / 20:13
0

Yes, you can use jQuery Deferred :

var defer = $.Deferred();
var x = defer.promise();
x.then(function(value){
  alert(value);
});
defer.resolve('pronto');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    
01.07.2015 / 20:14