|
1 |
| -# Node.js |
| 1 | +# Node.js, Events, and Callbacks |
2 | 2 |
|
3 | 3 | A computer program can run in many host environments.
|
4 | 4 |
|
@@ -91,30 +91,60 @@ event.on('update', function(data){
|
91 | 91 | console.log(data)
|
92 | 92 | })
|
93 | 93 | ```
|
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. |
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 |
| -// I literally cut and copied everything after the = from the previous example |
| 104 | +// I literally copied everything after the = from the previous example |
101 | 105 | var callback_Function = function(data){
|
102 | 106 | console.log(data)
|
103 | 107 | }
|
104 | 108 |
|
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 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 | + |
118 | 148 |
|
119 | 149 |
|
120 | 150 |
|
|
0 commit comments