0

First question is: What are callback functions?

Second question: How this code works and how it gets executed in JavaScript behind the scene?

The code:

// callback functions
const perOne = (friend, callfrnd) => {
  console.log(`I am busy right now. I am talking to ${friend}`);
  callfrnd();
};

const perTwo = () => {
  console.log(`Ok i will callback u later.`);
};

perOne("Kartik", perTwo);
VLAZ
  • 26,331
  • 9
  • 49
  • 67
coder
  • 1
  • 1
    A [callback function](https://developer.mozilla.org/en-US/docs/Glossary/Callback_function) is a function passed into another function as an argument, which is then invoked inside the outer function to complete some kind of routine or action. Usually used when asynchronous calls are completed. – Lain Oct 26 '22 at 13:01
  • Thank u so much but pls also tell me how that code just work and gets executed ? – coder Oct 26 '22 at 13:05
  • Use a debugger and step through the code line by line to see how it works. [Instructions for Chrome](https://stackoverflow.com/questions/10638059/javascript-debugging-line-by-line-using-google-chrome). – Heretic Monkey Oct 26 '22 at 13:05
  • Ok let me do it – coder Oct 26 '22 at 13:06
  • Also, please take the [tour] and read [ask]. Stack Overflow is not a forum or a mentoring site. It is a question-and-answer site. Single question, multiple answers, no chitchat. – Heretic Monkey Oct 26 '22 at 13:09
  • Ok sure i will be concerned about it from the next time – coder Oct 26 '22 at 13:23
  • `perOne` is called first with a string (`"Kartik"`) and function `perTwo` passed as arguments. Not all languages allow functions _themselves_ to be passed as arguments, but JS does. Inside `perOne`, parameter `callfrnd` now contains a reference to function `perTwo`; so inside `perOne` a string (`"I am busy right now..."`) is printed to the console, and then `callfrnd` (which is a reference to `perTwo`) is invoked (which prints `"Ok i will callback..."`). – Ben Aston Oct 26 '22 at 13:58
  • Ok thank u so much ben aston its mean alot to me – coder Oct 27 '22 at 04:48

0 Answers0