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

171
Visualizações
How can a callback parameter passed to a function be aware of that function's own return value?

Sorry if there's already other question around this subject. I couldn't find one.

Could somebody elaborate on how this code works?

I wasn't expecting the result from foo() to be available inside the callback declaration that is being passed as a parameter of foo.

What is the order of events that happens in a situation like this?

It's like the function returns first and only then it creates its own parameter? How does that make sense?

const foo = (callback) => {
  setTimeout(callback,500);
  return 42;
};

const result = foo(() => {
  console.log('From callback...');
  console.log('result:',result); 
})

Similar to this:

const id = setTimeout(() => console.log('id',id),500);

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

0

Firstly foo runs, which sets the callback to be run after 500ms, and then returns with 42. That gets stored in the result variable.

After 500ms the callback from foo's parameter gets called, and finds the variable result as a global variable and prints it out (which is set to be 42)

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