-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (56 loc) · 1.37 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// este codigo ya está guardado como promesasAsyncAwait.js
//veremos como crear una promesa
new Promise((resolve, reject) => {
resolve(5)
reject(new Error())
});
const toNumber = n => Number(n);
//const multiply = n => n * 2;
const multiply = n => {
console.log("Multiply", n);
if(n > 10) throw new Error("Error");
return n * 2;
}
const print = n => console.log(n);
const catchAndPrint = (err) => {
console.log(err);
return 1;
}
const request = (url) => {
return new Promise((resolve, reject) => {
const req = new XMLHttpRequest();
req.open("GET", url);
req.onload = () => {
if (req.status === 200){
resolve(req.responseText);
}
else{
reject(new Error('Error al cargar'));
}
}
req.onerror = () => {
reject(new Error("Error de red"));
}
req.send();
})
};
const prom = async () => {}
const call = async (url) => {
let response, response2;
try {
response = await request(url);
}catch (error) {
catchAndPrint(error);
response = 7;
}
try{
response2 = await request(url);
}catch (error) {
catchAndPrint(error);
response2 = 8;
}
const n1 = toNumber(response);
const n2 = toNumber(response2);
print(n1 * n2);
}
call("./numero2.txt");