Wait variable to be set to "open signaller"

Asked

Viewed 78 times

0

I have the variable x that goes through processes to be really defined and it takes a while, and to mess with the rest of the application I need it to be defined, that is I need to wait for it to be defined if not the project hangs..

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

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);
}

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>

Browser other questions tagged

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