0

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

3
  • 4
    "I call the callback function" – Uhm… where?! Commented Jan 25, 2019 at 15:05
  • 2
    You did not call the callback(). Commented Jan 25, 2019 at 15:05
  • 1
    call the callback after console.log(1); using callback(); Commented Jan 25, 2019 at 15:06

2 Answers 2

4

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)
})

Sign up to request clarification or add additional context in comments.

Comments

2

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);
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.