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
1/*
2A callback function is a function passed into another function
3as an argument, which is then invoked inside the outer function
4to complete some kind of routine or action.
5*/
6function greeting(name) {
7 alert('Hello ' + name);
8}
9
10function processUserInput(callback) {
11 var name = prompt('Please enter your name.');
12 callback(name);
13}
14
15processUserInput(greeting);
16// The above example is a synchronous callback, as it is executed immediately.
1/*
2tl;dr: Callbacks are a way to force one function to complete,
3before running another. Used for asynchronus programming.
4*/
5
6// Consider this example:
7
8names = ['Anthony', 'Betty', 'Camie']
9
10function addName(name, callback){
11 setTimeout(function(){
12 names.append(name)
13 callback()
14 }, 200)
15}
16
17function getNames(){
18 setTimeout(function(){
19 console.log(names)
20 }, 100)
21}
22
23addName('Daniel', getNames)
24/*
25
26addName('Daniel', getNames) is saying:
27
28'finish the function addName() BEFORE running the function getNames()'
29
30> 'getNames()' here is the callback
31
32Without the call back in this example, getNames would finish
33before addName. (setTimeout is used to force one function to go slower)
34
35Callbacks are given as arguments to other functions
36
37*/
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);
1/*
2tl;dr: Callbacks are a way to force one function to complete,
3before running another. Used for asynchronus programming.
4*/
5
6// Consider this example:
7
8names = ['Anthony', 'Betty', 'Camie']
9
10function addName(name, callback){
11 setTimeout(function(){
12 names.append(name)
13 callback()
14 }, 200)
15}
16
17function getNames(){
18 setTimeout(function(){
19 console.log(names)
20 }, 100)
21}
22
23addName('Daniel', getNames)
24/*
25addName('Daniel', getNames) is saying:
26'finish the function addName() BEFORE running the function getNames()'
27
28> 'getNames()' here is the callback
29
30Without the call back in this example, getNames would finish
31before addName. (setTimeout is used to force one function to go slower)
32
33Callbacks are given as arguments to other functions
34
35*/
1//Callback functions - are functions that are called AFTER something happened
2
3 const foo = (number, callbackFunction) => {
4 //first
5 console.log(number)
6
7 //second - runs AFTER console.log() happened
8 callbackFunction()
9 }
10