Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

104
Visualizações
Javascript Function Call have different behaviour compared to run by console in web browser

i have this code:

function foo(){
    var x = $.getJSON('./someFileName.txt',function(data){});
    var y = x.responseText;
    console.log(y);
}

when i called it via browser console (firefox), it give me undefined for the console log result.

But, when i just copy, paste and, run the foo() function body line by line in my browser console, it gives me a result which is a string.

Can anyone enlighten me why this behaviour happens? I suppose it has something to do with the jquery call but i am not sure.

Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to place the second and third lines of your function inside the callback like this:

function foo(){
    $.getJSON('./someFileName.txt',function(data){
    var y = data.responseText;
    console.log(y);
    }); 
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda