Skip to content

Commit 8322991

Browse files
committed
add code examples
1 parent 144050f commit 8322991

File tree

6 files changed

+61
-4
lines changed

6 files changed

+61
-4
lines changed

code/1.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// cd code/
2+
// node 1.js
3+
4+
var myNumber = 1
5+
function addOne() { myNumber++ } // define the function
6+
addOne() // run the function
7+
console.log(myNumber) // logs out 2

code/2.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// cd code/
2+
// node 2.js
3+
4+
var fs = require('fs')
5+
var myNumber = undefined // we dont know what the number is yet since it is stored in a file
6+
7+
function addOne() {
8+
fs.readFile('./number.txt', function doneReading(err, fileContents) {
9+
myNumber = parseInt(fileContents)
10+
myNumber++
11+
})
12+
}
13+
14+
addOne()
15+
16+
console.log(myNumber) // logs out undefined

code/3.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// cd code/
2+
// node 3.js
3+
4+
var fs = require('fs')
5+
var myNumber = undefined
6+
7+
function addOne(callback) {
8+
fs.readFile('number.txt', function doneReading(err, fileContents) {
9+
myNumber = parseInt(fileContents)
10+
myNumber++
11+
callback()
12+
})
13+
}
14+
15+
function logMyNumber() {
16+
console.log(myNumber)
17+
}
18+
19+
addOne(logMyNumber)

code/4.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function takesOneSecond(callback) {
2+
setTimeout(after, 1000)
3+
function after() {
4+
console.log('done with one, on to the next')
5+
if (callback) callback()
6+
}
7+
}
8+
9+
a = takesOneSecond, b = takesOneSecond, c = takesOneSecond
10+
11+
a(function() {
12+
b(function() {
13+
c()
14+
})
15+
})

code/number.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1

readme.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
# Art of Node
1+
# The Art of Node
22
## An introduction to Node.js
33

44
This document is intended for readers who know at least a little bit of a scripting language like JavaScript, Ruby, Python, Perl, etc. If you aren't a programmer yet then it is probably easier to start by reading [JavaScript for Cats](http://jsforcats.com/). :cat2:
55

6-
It is also currently a work in progress. If you like it then please consider donating via [gittip](https://www.gittip.com/maxogden/).
6+
It is also currently a work in progress. If you like it then please consider donating via [gittip](https://www.gittip.com/maxogden/) so that I can write more!
77

88
## Table of contents
99

@@ -101,7 +101,7 @@ function addOne() {
101101
fs.readFile('./number.txt', function doneReading(err, fileContents) {
102102
myNumber = parseInt(fileContents)
103103
myNumber++
104-
}
104+
})
105105
}
106106

107107
addOne()
@@ -173,7 +173,6 @@ TODO
173173
174174
## Streams
175175
176-
177176
Early on in the project the file system and network APIs had their own separate patterns for dealing with streaming I/O. For example, files in a file system have things called 'file descriptors' so the `fs` module had to have extra logic to keep track of these things whereas the network modules didn't have such a concept. Despite minor differences in semantics like these, at a fundamental level both groups of code were duplicating a lot of functionality when it came to reading data in and out. The team working on node realized that it would be confusing to have to learn two sets of semantics to essentially do the same thing so they made a new API called the `Stream` and made all the network and file system code use it.
178177
179178
THE REST IS TODO

0 commit comments

Comments
 (0)