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

108
Visualizações
Problem with a function taken as a function parameter that takes AJAX as its own parameter

I can console.log(arg) but not arg.response. Curious what is the reason behind // edit: when I console log arg I can clearly see response property with data

   function doAjax(param, lambda) {
        let _xhttp = new XMLHttpRequest();
        _xhttp.open("GET", param, true);
        _xhttp.send();
        _xhttp.onload = lambda(_xhttp);
    }
    
    doAjax("ALL_FRAMES", function(arg) {
         console.log(arg.response); --> prints blank field in console
         //allFrames = JSON.parse(arg.response);
    });
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

_xhttp.onload = lambda(_xhttp);

This means "call lambda(_xhttp) immediately, and then assign its return value to _xhttp.onload". You instead want to assign a function to onload, and that function will call lambda later on:

_xhtttp.onload = () => lambda(_xhttp);

// edit: when I console log arg I can clearly see response property with data

That is just a quirk of the way the dev tools handle logging objects. When logging an object, the object is not evaluated until you click on the object in your dev tools. So at the time the console.log ran it didn't have the response yet, but by the time you click, it does have it.

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