Skip to content
This repository was archived by the owner on Sep 15, 2025. It is now read-only.

Commit 88952c2

Browse files
committed
Examples
0 parents  commit 88952c2

File tree

16 files changed

+414
-0
lines changed

16 files changed

+414
-0
lines changed

.gitignore

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

01.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// Understanding Async Nature
3+
//
4+
function insert(id, callback) {
5+
setTimeout(function() {
6+
console.log('Insertando ' + id);
7+
callback('Exitoso');
8+
}, 3000);
9+
}
10+
11+
console.log('Op1');
12+
insert(1, function(result) {
13+
console.log(result);
14+
});
15+
console.log('Op2');

02.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//
2+
// 02 - Pyramid of Doom / Callback Hell/Soup
3+
//
4+
console.log('Inicio');
5+
step1(function(result1) {
6+
step2(function(result2) {
7+
step3(function(result3) {
8+
step4(function(result4) {
9+
console.log('Result is: '+ result4);
10+
});
11+
});
12+
});
13+
});
14+
console.log('Fin');
15+
16+
function step1(callback) {
17+
setTimeout(function () {
18+
console.log('Hello step 1');
19+
callback(1);
20+
}, (Math.random() * 1000));
21+
}
22+
23+
function step2(callback) {
24+
setTimeout(function () {
25+
console.log('Hello step 2');
26+
callback(2);
27+
}, (Math.random() * 1000));
28+
}
29+
30+
function step3(callback) {
31+
setTimeout(function () {
32+
console.log('Hello step 3');
33+
callback(3);
34+
}, (Math.random() * 1000));
35+
}
36+
37+
function step4(callback) {
38+
setTimeout(function () {
39+
console.log('Hello step 4');
40+
callback(4);
41+
}, (Math.random() * 1000));
42+
}

03.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// 03 - Common Problems
3+
//
4+
function checkPassword(username, passwordGuess, callback) {
5+
var queryStr = 'SELECT * FROM user WHERE username = ?';
6+
db.query(selectUser, username, function (err, result) {
7+
if (err) throw err;
8+
hash(passwordGuess, function(passwordGuessHash) {
9+
callback(passwordGuessHash === result.password_hash);
10+
});
11+
});
12+
}
13+
14+
function checkPassword(username, passwordGuess, callback) {
15+
var passwordHash;
16+
var queryStr = 'SELECT * FROM user WHERE username = ?';
17+
db.query(selectUser, username, queryCallback);
18+
19+
function queryCallback(err, result) {
20+
if (err) throw err;
21+
passwordHash = result.password_hash;
22+
hash(passwordGuess, hashCallback);
23+
}
24+
25+
function hashCallback(passwordGuessHash) {
26+
callback(passwordHash === passwordGuessHash);
27+
}
28+
}

04.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// PubSub
3+
//
4+
5+
var EE = new require('events').EventEmitter;
6+
7+
var emitter = new EE();
8+
9+
(function() {
10+
setTimeout(function() {
11+
emitter.emit('action', 'Hello Med.js');
12+
}, 5000);
13+
})(); // Remember IIFE
14+
15+
emitter.on('action', function (params) {
16+
console.log(params);
17+
});
18+
19+
emitter.on('action', function (params) {
20+
console.log('I can execute more actions!!');
21+
});
22+
23+
// jQuery http://jsfiddle.net/jcsdD/

05-server.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
var http = require('http'),
2+
fs = require('fs');
3+
4+
http.createServer(function (req, res) {
5+
res.writeHead(200, { 'Content-Type': 'text/html'});
6+
switch (req.url) {
7+
case '/step1':
8+
res.write(JSON.stringify({ step: 1 }));
9+
res.end();
10+
break;
11+
case '/step2':
12+
res.write(JSON.stringify({ step: 2 }));
13+
res.end();
14+
break;
15+
default:
16+
fs.readFile(__dirname + '/05.html', 'utf-8', function (err, file) {
17+
if (err) throw err;
18+
res.write(file);
19+
res.end();
20+
});
21+
}
22+
23+
}).listen(8080);
24+
console.log('Running');

05.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset='utf-8' />
5+
<title>Demo</title>
6+
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
7+
<script>
8+
9+
$.ajax({
10+
method: 'get',
11+
url: 'http://localhost:8080/step1',
12+
success: successHandler,
13+
error: failHandler
14+
});
15+
16+
$.get('http://localhost:8081/step1').
17+
done(successHandler).
18+
fail(failHandler);
19+
20+
function successHandler(response) {
21+
console.log(response);
22+
}
23+
24+
function failHandler(response) {
25+
console.log(response);
26+
}
27+
</script>
28+
</head>
29+
<body>
30+
<p>Hello this is demo, see the debug console!</p>
31+
</body>
32+
</html>

05.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// Promises
3+
//
4+
var Q = require('q'),
5+
fs = require('fs');
6+
7+
readFile('05.js').
8+
then(fileHandler).
9+
fail(failHandler);
10+
11+
12+
function readFile(filename) {
13+
var fileReading = Q.defer();
14+
fs.readFile(filename, 'utf-8',
15+
fileReading.makeNodeResolver());
16+
return fileReading.promise;
17+
}
18+
19+
function failHandler (err) {
20+
console.error(err);
21+
}
22+
23+
function fileHandler(file) {
24+
console.log(file);
25+
}

06.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// Async.js
3+
//
4+
5+
function insert(id, callback) {
6+
setTimeout(function() {
7+
console.log('Inserting ' + id);
8+
callback();
9+
}, (Math.random() * 1000));
10+
}
11+
12+
for(var i = 0; i < 10; i++) {
13+
insert(i, function() {
14+
console.log('Im done with' + i);
15+
});
16+
}

07.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var fs = require('fs');
2+
var concatenation = '';
3+
fs.readdirSync('.')
4+
.filter(function(filename) {
5+
// ignore directories
6+
return fs.statSync(filename).isFile();
7+
}).forEach(function(filename) {
8+
// add contents to our output
9+
concatenation += fs.readFileSync(filename, 'utf8');
10+
});
11+
console.log(concatenation);

0 commit comments

Comments
 (0)