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

273
Vistas
There is any difference between callback function and calling a function inside another function?

Let's take this example:

function foo(callback){
        callback();
        baz();
  }

  function baz() {
        console.log('Hello from baz');  
   }

   foo(baz);

It looks like they are doing the same task. Or callback and calling a function inside another function are the same thing?

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Basically, they are doing the same, but callbacks are very useful because you can call the same function with many different callbacks. That makes your foo function more reusable.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Except you aren't using the callback the way callbacks are supposed to be used. Callbacks are supposed to get the interim results from a previous function. Here's an example

function add (a, b) {
  return a + b;
}

function subtract (a, b) {
  return a - b;
}

This is the callback

function doLater (val) {
  if (val > 0) {
    console.log("positive");
  } else if (val ==) {
    console.log("zero");
  } else console.log ("negative");
}

function doOp (a, b, mathOp, callback) {
  var res = mathOp(a,b);
  callback(res);
}

doOp (2,3, add, doLater);

vs.

doOp (2,3,subtract, doLater);
about 4 years ago · Juan Pablo Isaza Denunciar

0

In your example, both approaches will do the same.

However, you can pass function as parameters, that are not in the functions scope. E.g.

function foo(callbackFn){
    callbackFn();
    // function test is undefined here, since it was declared in another scope 
}

function bar(){
    // test is only available in bar's scope
    function test(){
        console.log("test");
    }
    foo(bar);
}
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