Santosh
Santosh

Reputation: 3827

Callback function is not working in javascript

I have read a few articles regarding callback function. I understand how they presented like add a + b then give callback function. But I am doing same. I first declared the function then call it again I call the callback function, why it is not working in my case?

function me(callback){
  console.log("1")
}
me(function(){ 
  console.log(2)
})

I am expecting console.log 1 then console.log 2. I am getting only console.log 1

Upvotes: 0

Views: 1852

Answers (2)

Davi
Davi

Reputation: 4147

You have to actually call the callback function inside the function it is passed to as argument:

function me(callback){
  console.log(1)
  callback();
}

me(function(){ 
  console.log(2);
})

Upvotes: 2

Prince Hernandez
Prince Hernandez

Reputation: 3721

you are calling the callback function, it won't trigger automatically, that approach is so you can notify something using that callback function when your function ended something.

function me(callback) {
  console.log("1")

  // your process ended, lets notify
  callback();
}
me(function() {
  console.log(2)
})

Upvotes: 4

Related Questions