Skip to content

Commit 88952c2

Browse files
committedMar 1, 2013
Examples
0 parents  commit 88952c2

16 files changed

+414
-0
lines changed
 

Diff for: ‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

Diff for: ‎01.js

+15
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');

Diff for: ‎02.js

+42
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+
}

Diff for: ‎03.js

+28
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+
}

Diff for: ‎04.js

+23
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/

Diff for: ‎05-server.js

+24
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');

Diff for: ‎05.html

+32
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>

Diff for: ‎05.js

+25
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+
}

Diff for: ‎06.js

+16
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+
}

Diff for: ‎07.js

+11
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);

Diff for: ‎07a.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var fs = require('fs');
2+
var concatenation = '';
3+
fs.readdir('.', function(err, filenames) {
4+
if (err) throw err;
5+
6+
function readFileAt(i) {
7+
var filename = filenames[i];
8+
console.log(filename);
9+
if (typeof filename !== 'undefined') {
10+
fs.stat(filename, function(err, stats) {
11+
if (err) throw err;
12+
if (!stats.isFile()) return readFileAt(i + 1);
13+
14+
fs.readFile(filename, 'utf8', function(err, text) {
15+
if (err) throw err;
16+
concatenation += text;
17+
18+
if (i + 2 === filenames.length) {
19+
// all files read, display the output
20+
return console.log(concatenation);
21+
}
22+
23+
readFileAt(i + 1);
24+
});
25+
});
26+
}
27+
}
28+
readFileAt(0);
29+
});

Diff for: ‎07b.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var async = require('async');
2+
var fs = require('fs');
3+
4+
var concatenation = '';
5+
var dirContents = fs.readdirSync('.');
6+
7+
async.filter(dirContents, isFilename, function(filenames) {
8+
async.eachSeries(filenames, readAndConcat, onComplete);
9+
});
10+
11+
function isFilename(filename, callback) {
12+
fs.stat(filename, function(err, stats) {
13+
if (err) throw err;
14+
callback(stats.isFile());
15+
});
16+
}
17+
18+
function readAndConcat(filename, callback) {
19+
fs.readFile(filename, 'utf8', function(err, fileContents) {
20+
if (err) return callback(err);
21+
concatenation += fileContents;
22+
callback();
23+
});
24+
}
25+
26+
function onComplete(err) {
27+
if (err) throw err;
28+
console.log(concatenation);
29+
}

Diff for: ‎08.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// Async.js
3+
//
4+
var async = require('async');
5+
6+
function insert(id, callback) {
7+
setTimeout(function() {
8+
console.log('Inserting ' + id);
9+
callback(null, id);
10+
}, (Math.random() * 1000));
11+
}
12+
13+
function filter(data, callback) {
14+
callback(data < 3);
15+
}
16+
17+
var data = [0, 1, 2, 3, 4, 5];
18+
19+
async.filter(data, filter, function(filteredData) {
20+
async.eachSeries(filteredData, insert, function(err) {
21+
if (err) throw err;
22+
console.log('Finished');
23+
});
24+
});
25+
26+
/*async.each(data, insert, function(err) {
27+
if (err) throw err;
28+
console.log('Finished');
29+
});*/
30+
31+
32+
33+
34+
35+
36+

Diff for: ‎09.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var async = require ('async');
2+
var start = new Date();
3+
4+
async.parallel([
5+
function(callback) { setTimeout(callback, 100); },
6+
function(callback) { setTimeout(callback, 300); },
7+
function(callback) { setTimeout(callback, 200); }
8+
], function(err, results) {
9+
// show time elapsed since start
10+
console.log('Completed in ' + (new Date() - start) + 'ms');
11+
});

Diff for: ‎09a.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// 02 - Pyramid of Doom / Callback Hell/Soup
3+
//
4+
var async = require('async');
5+
6+
async.parallel([
7+
function(callback) {
8+
console.log('Inicio');
9+
callback(null, 0);
10+
},
11+
step1,
12+
step2,
13+
step3,
14+
step4
15+
], function(err, results) {
16+
console.log('Result is: ' + results);
17+
console.log('Fin');
18+
});
19+
20+
function step1(callback) {
21+
setTimeout(function () {
22+
console.log('Hello step 1');
23+
callback(null, 'Hola');
24+
}, (Math.random() * 1000));
25+
}
26+
27+
function step2(callback) {
28+
setTimeout(function () {
29+
console.log('Hello step 2');
30+
callback(null, 'Mundo');
31+
}, (Math.random() * 1000));
32+
}
33+
34+
function step3(callback) {
35+
setTimeout(function () {
36+
console.log('Hello step 3');
37+
callback(null, 'Soy Async');
38+
}, (Math.random() * 1000));
39+
}
40+
41+
function step4(callback) {
42+
setTimeout(function () {
43+
console.log('Hello step 4');
44+
callback(null, 4);
45+
}, (Math.random() * 1000));
46+
}

Diff for: ‎09b.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// 02 - Pyramid of Doom / Callback Hell/Soup
3+
//
4+
var async = require('async');
5+
6+
async.waterfall([
7+
function(callback) {
8+
console.log('Inicio');
9+
callback(null, 0);
10+
},
11+
step1,
12+
step2,
13+
step3,
14+
step4
15+
], function(err, results) {
16+
console.log('Result is: ' + results);
17+
console.log('Fin');
18+
});
19+
20+
function step1(number, callback) {
21+
setTimeout(function () {
22+
console.log('Hello step 1');
23+
callback(null, 1);
24+
}, (Math.random() * 1000));
25+
}
26+
27+
function step2(number, callback) {
28+
setTimeout(function () {
29+
console.log('Hello step 2');
30+
callback(null, 2);
31+
}, (Math.random() * 1000));
32+
}
33+
34+
function step3(number, callback) {
35+
setTimeout(function () {
36+
console.log('Hello step 3');
37+
callback(null, 3);
38+
}, (Math.random() * 1000));
39+
}
40+
41+
function step4(number, callback) {
42+
setTimeout(function () {
43+
console.log('Hello step 4');
44+
callback(null, 'Resultado final');
45+
}, (Math.random() * 1000));
46+
}

0 commit comments

Comments
 (0)
Please sign in to comment.