Skip to content

Commit b3c3821

Browse files
committed
Eventos, Conc, file, streams
1 parent 7f4d9b7 commit b3c3821

17 files changed

Lines changed: 207 additions & 0 deletions

e50-timeout.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
setTimeout(function() { // callback
2+
console.log('world');
3+
},
4+
2000 // evento ocurre 2000
5+
); // miliseg. despues
6+
7+
8+
console.log('hello');

e52-file.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var fs = require('fs');
2+
3+
fs.readFile('file.js',
4+
'ascii',
5+
function(err, data) {
6+
console.log(data)
7+
}
8+
);

e53-file-bin.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var fs = require('fs');
2+
var filehandle = fs.readFile('e04-file-bin.js',
3+
function(err, data) {
4+
console.log(data)
5+
});

e55-argv.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var i;
2+
for(i = 0; i < process.argv.length; i++) {
3+
console.log('Parametro ' + i + " = " + process.argv[i]) ;
4+
}

e56-argv.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var i;
2+
for(i = 0; i < process.argv.length; i++) {
3+
console.log('Parametro ' + i + " = " + process.argv[i]) ;
4+
}
5+
6+
// programa equivalente con método de JavaScript 1.5
7+
process.argv.forEach(function (val, index, array) {
8+
console.log('Parametro ' + index + ': ' + val);
9+
});

e57-uncaughtException.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// define manejador de evento
2+
process.on('uncaughtException', function(err) {
3+
console.log('EXCEPCION CAPTURADA: \n -> ' + err);
4+
});
5+
6+
console.log('Este mensaje saldra por consola');
7+
8+
// No estar definida, disparará uncaughtException
9+
funcionIndefinida();
10+
11+
console.log('Este mensaje NO saldra por consola');

e60-nextTick.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
setTimeout(function() { console.log('Evento A');}, 2);
2+
setTimeout(function() { console.log('Evento B');}, 0);
3+
4+
process.nextTick(function() { console.log('Tick D');});
5+
process.nextTick(function() { console.log('Tick E');});
6+
console.log('Fin de Programa Principal');

e61-deadlock.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var i, fact = 1;
2+
for(i = 1; i == 10; i=i+2) { // error: bucle infinito
3+
fact = fact*i; // bloquea procesador
4+
console.log(i + "! = " + fact) ;
5+
}
6+
7+
// no se ejecutarian nunca los eventos

e65-mutual_exclusion.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var tiempo = 6000; // variable compartida
2+
3+
setInterval(function() {
4+
tiempo = tiempo - 1000;
5+
console.log('A consume: 1000');
6+
if (tiempo < 1) {process.exit()};
7+
},
8+
1000
9+
);
10+
11+
setInterval(function() {
12+
tiempo = tiempo - 1000;
13+
console.log('B consume: 1000');
14+
if (tiempo < 1) {process.exit()};
15+
},
16+
1000
17+
);

e70-copy.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var fs = require('fs'); // importa modulo file system
2+
3+
if (process.argv.length != 4){ // parametros mal?
4+
console.log(' syntax: "node copy <orig> <dest>"');
5+
process.exit() // finaliza proceso node
6+
}
7+
8+
fs.readFile(
9+
process.argv[2], // fichero <orig>
10+
function(err, data) { // callback de finalización
11+
fs.writeFile(
12+
process.argv[3], // fichero <dest>
13+
data, // contenido de <orig>
14+
function (err) { // callback de finalización
15+
if (err) throw err;
16+
console.log('file copied');
17+
}
18+
);
19+
}
20+
);

0 commit comments

Comments
 (0)