Skip to content

Commit 7cc077e

Browse files
committed
fetch api
1 parent 40c74d5 commit 7cc077e

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

Ejemplos/async-await.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Document</title>
8+
</head>
9+
<body>
10+
11+
12+
<script>
13+
14+
async function obtenerClientes(){
15+
const clientes = new Promise((resolve,reject) =>{
16+
setTimeout(() =>{
17+
resolve(`Clientes descargados...`);
18+
},2000);
19+
});
20+
21+
const error = false;
22+
23+
if(!error){
24+
const respuesta = await clientes;
25+
return respuesta;
26+
}else{
27+
await Promise.reject(`Hubo un error...`);
28+
}
29+
}
30+
31+
obtenerClientes()
32+
.then(res => console.log(res))
33+
.catch(error => console.log(error));
34+
35+
</script>
36+
</body>
37+
</html>

Ejemplos/async-await_2.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<meta http-equiv="X-UA-Compatible" content="ie=edge">
7+
<title>Document</title>
8+
</head>
9+
<body>
10+
11+
<script>
12+
async function leerTodos(){
13+
const respuesta = await fetch('https://jsonplaceholder.typicode.com/todos');
14+
15+
const datos = await respuesta.json();
16+
17+
return datos;
18+
}
19+
20+
//console.log(leerTodos());
21+
22+
leerTodos()
23+
.then(usuarios => console.log(usuarios));
24+
</script>
25+
</body>
26+
</html>

0 commit comments

Comments
 (0)