Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

170
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda