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
To the uninitiated, this may look like a pinata of inscruotable syntax; and hlaf of it comprises the callback.
94
+
To the uninitiated, this may look like a pinata of inscruotable syntax.
95
95
96
-
There, a function is being called, which takes two arguments. One argument is the string, 'update'. The next argument is a function. This is confusing because that function is written out within the parenthesis of the function being called.
96
+
One reason is because that code has nested syntax, which is totally legal.
97
97
98
-
Look at another way to write the same thing:
98
+
Half of that code is a callback.
99
+
100
+
On the outside, a function is being called, which takes two arguments between its parenthesis. One argument is a string, 'update'. The next argument is a function, written out. This is confusing because that function is written out within the parenthesis of the function being called.
101
+
102
+
However, look at another way to write the same thing:
99
103
```js
100
104
// I literally copied everything after the = from the previous example
101
105
varcallback_Function=function(data){
@@ -105,16 +109,40 @@ var callback_Function = function(data){
105
109
event.on('update', callback_Function)
106
110
```
107
111
108
-
Here, you can see that the callback is a basic function, and event.on is a function being called with two arguments, one of them a function.
112
+
Here, you can see that the callback is a basic function, and event.on is a function being called with two arguments, one of them the function referenced aboove.
109
113
110
-
When an 'update' event happens, call the supplied function, in this case 'callback_Function', which was reference above.
114
+
When an 'update' event happens, call the supplied function.
111
115
112
116
It is very common in javascript to supply a function as an argument to another function, like in real life you might give directions "in the event" something occurs.
113
117
118
+
As with all writing, code can get sloppy; we often forgetting, or ignoring, the fact that other humans will actually try to read and understand this.
119
+
114
120
Node.js is built on top of events. This is because there is often some time between the initiation of a sequence, and it the events which are produced; for instance when you fetch data from a far away server, and wait for the response.
115
121
116
122
so much for events and callbacks.
117
123
124
+
# psyche!
125
+
126
+
Events and callbacks are a paradigm for handling asyncronous behaviours.
127
+
128
+
One asyncronous behaviour is your very own: you write the code before it runs.
129
+
130
+
The other async behaviour occurs naturally as a result of time and space.
131
+
132
+
Asyncronous is the word.
133
+
134
+
This guide did nothing say of how to deal with asyncronous behaviours.
135
+
136
+
Async behaviours did not come natural to most early programmers of other languages, but that doesn't have to be the case with you.
137
+
138
+
There are many styles to try writing javascript, to wend your way about intersecting streams of data and outer userspace.
139
+
140
+
Thread the needle.
141
+
142
+
or, try the [sweet bun style](https://www.youtube.com/watch?v=HtBebT-rKeM&list=PLYgHYEWMPzcrzcbGGqJyTKE9FK6DLTCiw)
0 commit comments