Skip to content

Commit 1c89512

Browse files
committed
Ejemplos sobre Asincronía
1 parent 6d49f11 commit 1c89512

11 files changed

+124
-0
lines changed

2023-03-16/callback-chain.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const asum = (a, b, callback) => {
2+
setTimeout(() => callback(a + b), 1000);
3+
};
4+
const asub = (a, b, callback) => {
5+
setTimeout(() => callback(a - b), 1000);
6+
};
7+
const amul = (a, b, callback) => {
8+
setTimeout(() => callback(a * b), 1000);
9+
};
10+
11+
// 5 * (4 + (10 - 7))
12+
// mul(5, sum(4, sub(10, 7)))
13+
14+
asub(10, 7, (r1) => {
15+
asum(4, r1, (r2) => {
16+
amul(5, r2, (resultado) => {
17+
console.log("Resultado:", resultado);
18+
});
19+
});
20+
});

2023-03-16/fetch-await.mjs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
const loadUser = async () => {
3+
let response;
4+
try {
5+
response = await fetch(`https://randomuser.me/api`);
6+
} catch (e) {
7+
return null;
8+
}
9+
let json;
10+
try {
11+
json = await response.json();
12+
} catch (e) {
13+
return null;
14+
}
15+
return json.results[0];
16+
}
17+
18+
let { email, gender, name } = await loadUser();
19+
console.log(`${email}, ${gender}, ${name.first} ${name.last}`);

2023-03-16/fetch-promise-chain.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
fetch(`https://randomuser.me/apizzzz`)
2+
.then((response) => response.json())
3+
.then((json) => console.log(json.results[0].email))
4+
.catch((err) => console.error("He cazado el error:", err));

2023-03-16/fich1.txt

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

2023-03-16/fich2.txt

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

2023-03-16/read-file.mjs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
import fs from 'fs';
3+
4+
fs.readFile('read-file.mjs', (err, data) => {
5+
if (err) {
6+
console.log("Error: ", err);
7+
} else {
8+
console.log("Ya tengo los datos", data);
9+
}
10+
});
11+
12+
console.log("Final");
13+

2023-03-16/read-two-files.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
import fs from 'fs';
3+
4+
fs.readFile('fich1.txt', (err, data1) => {
5+
if (err) {
6+
console.error(err);
7+
return;
8+
}
9+
const filename = data1.toString().trim();
10+
fs.readFile(filename, (err, data2) => {
11+
if (err) {
12+
console.error(err);
13+
return;
14+
}
15+
const content = data2.toString();
16+
console.log("Contenido del fichero 2:", content);
17+
})
18+
});

2023-03-16/set-timeout-0.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
console.log("Begin");
2+
setTimeout(
3+
() => console.log("Aquí!!"),
4+
0
5+
);
6+
console.log("End");

2023-03-16/suma-async.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
async function suma(a: number, b: number) {
3+
return a + b;
4+
}
5+
6+
const asuma = async (a:number, b: number) => a + b;

2023-03-16/suma-callback.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
function sumaSync(a: number, b: number): number {
3+
return a + b;
4+
}
5+
6+
function suma(a: number, b: number, callback: (r: number) => void) {
7+
setTimeout(() => callback(a + b), 2000);
8+
}
9+
10+
console.log("Empieza la función");
11+
12+
let r1 = sumaSync(10, 20);
13+
console.log("Resultado 1:", r1);
14+
15+
// La función suma no espera a tener el resultado y seguimos con el programa
16+
suma(10, 20, (r2) => {
17+
console.log("Resultado 2:", r2);
18+
});
19+
20+
console.log("Termina la función");
21+

0 commit comments

Comments
 (0)