Skip to content

Commit b213b35

Browse files
author
James Halliday
committed
Merge branch 'master' of github.com:cyberwizardinstitute/workshops
2 parents c851ce7 + e7cf979 commit b213b35

File tree

1 file changed

+37
-7
lines changed

1 file changed

+37
-7
lines changed

node_101.md

+37-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Node.js
1+
# Node.js, Events, and Callbacks
22

33
A computer program can run in many host environments.
44

@@ -91,30 +91,60 @@ event.on('update', function(data){
9191
console.log(data)
9292
})
9393
```
94-
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.
9595

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.
9797

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:
99103
```js
100-
// I literally cut and copied everything after the = from the previous example
104+
// I literally copied everything after the = from the previous example
101105
var callback_Function = function(data){
102106
console.log(data)
103107
}
104108

105109
event.on('update', callback_Function)
106110
```
107111

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.
109113

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.
111115

112116
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.
113117

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+
114120
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.
115121

116122
so much for events and callbacks.
117123

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 space, with its dimensions, which allows time to spread out over distance.
131+
132+
Asyncronous is the word, and it means "out of time", relative to other events a happening.
133+
134+
Yet a program may need to coordinate results from many asynconous events.
135+
136+
This is an apsect of your program's design; most programs can be written without knots of asyncronous events.
137+
138+
This guide does nothing say of how to write for asyncronous behaviours.
139+
140+
Async behaviours did not come natural to many programmers of other languages, and they could not handle it; but that doesn't have to be the case with you.
141+
142+
There are many styles to try writing javascript, to wend intersecting streams of data in outer userspace.
143+
144+
Thread the needle, or, try the [sweet bun style](https://www.youtube.com/watch?v=HtBebT-rKeM&list=PLYgHYEWMPzcrzcbGGqJyTKE9FK6DLTCiw)
145+
146+
147+
118148

119149

120150

0 commit comments

Comments
 (0)