You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on May 14, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: Week2/LESSONPLAN.md
+41-6Lines changed: 41 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -24,19 +24,54 @@ Notes:
24
24
- Asynchronous helps us do multiple things in parallel
25
25
26
26
27
-
## 3. What callbacks are and how to write your own
27
+
## 3. Callbacks
28
+
28
29
### Explanation
30
+
31
+
A callback in JavaScript is basically a function(callback) being passed as a parameter to another function which after some point of time would execute the function passed or invoke the callback.
32
+
33
+
Callbacks were primarily introduced in JavaScript to achieve asynchronous behaviour
Consider a situation where person A wishes to go out for a movie with a friend person B one evening. Person A finds out the time and place and now needs to share it with B. A picks up the phone and tries to call B. Let's say that B is currently busy with some work and can't answer the phone. Person A has now got two options. One option is to stay on the line until B picks up the phone and then share the movie details. Or A could drop a voicemail and ask B to __callback__ once free.
37
+
38
+
```javascript
39
+
functiondoHomework(subject, callback) {
40
+
alert(`Starting my ${subject} homework.`);
41
+
callback();
42
+
}
43
+
functionalertFinished(){
44
+
alert('Finished my homework');
45
+
}
46
+
doHomework('math', alertFinished);
47
+
```
48
+
30
49
### Exercise
31
-
### Essence
32
-
Notes:
33
50
34
-
- Callbacks are a way to introduce asynchronocity
51
+
#### 1. What will happen?
52
+
#### 2. How to turn the output order around?
53
+
```javascript
54
+
functionfirst(){
55
+
// Simulate a code delay
56
+
setTimeout( function(){
57
+
console.log(1);
58
+
}, 500 );
59
+
}
60
+
functionsecond(){
61
+
console.log(2);
62
+
}
63
+
64
+
first();
65
+
second();
66
+
```
35
67
36
68
69
+
### Essence
70
+
you can’t just call one function after another and hope they execute in the right order. Callbacks are a way to make sure certain code doesn’t execute until other code has already finished execution.
71
+
37
72
SECOND HALF (14.00 - 16.00)
38
73
39
-
## 4. How the event loop works
74
+
## 4. Event loops
40
75
41
76
### Explanation
42
77
### Example
@@ -48,7 +83,7 @@ Notes:
48
83
- It determines when any given function is executed
49
84
50
85
51
-
## 5. Show 3 commonly used array functions (filter, reduce, map)
86
+
## 5. 3 commonly used array functions (filter, reduce, map)
0 commit comments