js callback function

JavaScript
/*
A 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. 
*/
function greeting(name) {
  alert('Hello ' + name);
}

function processUserInput(callback) {
  var name = prompt('Please enter your name.');
  callback(name);
}

processUserInput(greeting);
// The above example is a synchronous callback, as it is executed immediately.// Create a callback in the probs, in this case we call it 'callback'
function newCallback(callback) {
  callback('This can be any value you want to return')
}

// Do something with callback (in this case, we console log it)
function actionAferCallback (callbackData) {
  console.log(callbackData)
}

// Function that asks for a callback from the newCallback function, then parses the value to actionAferCallback
function requestCallback() {
  newCallback(actionAferCallback)
}function greeting(name) {
  alert('Hello ' + name);
}

function processUserInput(callback) {
  var name = prompt('Please enter your name.');
  callback(name);
}

processUserInput(greeting);Events - Think of a Server (Employee) and Client (Boss).
One Employee can have many Bosses.
The Employee Raises the event, when he finishes the task,
and the Bosses may decide to listen to the Employee event or not.
The employee is the publisher and the bosses are subscriber.

Callback - The Boss specifically asked the employee to do a task
and at the end of task done,
the Boss wants to be notified. The employee will make sure that when the task
is done, he notifies only the Boss that requested, not necessary all the Bosses.
The employee will not notify the Boss, if the partial job is done.
It will be only after all the task is done. 
Only one boss requested the info, and employee only posted the 
reply to one boss.

R: https://stackoverflow.com/a/34247759/7573706function add(a, b, callback) {
  if (callback && typeof(callback) === "function") {
    callback(a + b);
  }
}

add(5, 3, function(answer) {
  console.log(answer);
});const add = (num1, num2) => num1 + num2;

const result = (num1, num2, cb) => {
  return "result is:" + cb(num1, num2);
}

const res = result(12, 13, add);
Source

Also in JavaScript: