1Events - Think of a Server (Employee) and Client (Boss).
2One Employee can have many Bosses.
3The Employee Raises the event, when he finishes the task,
4and the Bosses may decide to listen to the Employee event or not.
5The employee is the publisher and the bosses are subscriber.
6
7Callback - The Boss specifically asked the employee to do a task
8and at the end of task done,
9the Boss wants to be notified. The employee will make sure that when the task
10is done, he notifies only the Boss that requested, not necessary all the Bosses.
11The employee will not notify the Boss, if the partial job is done.
12It will be only after all the task is done.
13Only one boss requested the info, and employee only posted the
14reply to one boss.
15
16R: https://stackoverflow.com/a/34247759/7573706
1function greeting(name) {
2 alert('Hello ' + name);
3}
4
5function processUserInput(callback) {
6 var name = prompt('Please enter your name.');
7 callback(name);
8}
9
10processUserInput(greeting);
1function startWith(message, callback){
2 console.log("Clearly written messages is: " + message);
3
4 //check if the callback variable is a function..
5 if(typeof callback == "function"){
6 callback(); //execute function if function is truly a function.
7 }
8}
9//finally execute function at the end
10startWith("This Messsage", function mySpecialCallback(){
11 console.log("Message from callback function");
12})
1function add(a, b, callback) {
2 if (callback && typeof(callback) === "function") {
3 callback(a + b);
4 }
5}
6
7add(5, 3, function(answer) {
8 console.log(answer);
9});
1function greeting(name) {
2 alert('Hello ' + name);
3}
4
5function processUserInput(callback , {
6 var name = prompt('Please enter your name.');
7 callback(name);
8}}
9
10processUserInput(greeting);
1const add = (num1, num2) => num1 + num2;
2
3const result = (num1, num2, cb) => {
4 return "result is:" + cb(num1, num2);
5}
6
7const res = result(12, 13, add);