Skip to content

Commit 7b3add7

Browse files
committed
exercicio async await
1 parent b966614 commit 7b3add7

File tree

4 files changed

+91
-20
lines changed

4 files changed

+91
-20
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
@import url('https://fonts.googleapis.com/css2?family=Quicksand:wght@400;500;600&display=swap');
2+
3+
body {
4+
display: flex;
5+
align-items: center;
6+
justify-content: center;
7+
font-family: 'Quicksand', sans-serif;
8+
height: 100vh;
9+
margin: 0;
10+
background-color: beige;
11+
}
12+
13+
img {
14+
max-width: 600px;
15+
border-radius: 30px;
16+
}
17+
18+
main {
19+
text-align: center;
20+
}
21+
22+
button {
23+
border: 0;
24+
font-size: 1.2rem;
25+
font-weight: 500;
26+
padding: 16px;
27+
background: orangered;
28+
color: white;
29+
margin: 20px 0;
30+
cursor: pointer;
31+
transition: all 0.5s ease-in-out;
32+
}
33+
34+
button:hover {
35+
background: orange;
36+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const BASE_URL = 'https://thatcopy.pw/catapi/rest/';
2+
3+
const getCats = async () => {
4+
try {
5+
const data = await fetch(BASE_URL);
6+
const json = await data.json();
7+
return json.url;
8+
} catch (e) {
9+
console.log(e.message);
10+
}
11+
};
12+
13+
const loadImg = async () => {
14+
const img = document.getElementsByTagName('img')[0];
15+
img.src = await getCats();
16+
};
17+
18+
loadImg();
19+
20+
const btn = document.getElementById('change-cat');
21+
btn.addEventListener('click', loadImg);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<link rel="stylesheet" href="assets/css/styles.css" />
8+
<title>Just... cats!</title>
9+
</head>
10+
<body>
11+
<main>
12+
<h1>Just... cats!</h1>
13+
<img id="cat" /><br />
14+
<button id="change-cat">Change cat</button>
15+
</main>
16+
<script src="assets/js/scripts.js"></script>
17+
</body>
18+
</html>

playground.js

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
function calculaIdade(anos) {
2-
return `Daqui a ${anos} anos, ${this.nome} terá ${
3-
this.idade + anos
4-
} anos de idade.`;
5-
}
6-
7-
const pessoa1 = {
8-
nome: 'Maria',
9-
idade: 30,
10-
};
1+
async function resolvePromise() {
2+
const myPromise = new Promise((resolve, reject) => {
3+
window.setTimeout(() => {
4+
resolve('Resolvida');
5+
}, 3000);
6+
});
117

12-
const pessoa2 = {
13-
nome: 'Carla',
14-
idade: 26,
15-
};
8+
let result;
169

17-
const animal = {
18-
nome: 'Fiona',
19-
idade: 5,
20-
raca: 'Pug',
21-
};
10+
try {
11+
result = await myPromise
12+
.then((result) => result + ' passando pelo then')
13+
.then((result) => result + ' e agora acabou!');
14+
} catch (err) {
15+
result = err.message;
16+
}
2217

23-
console.log(calculaIdade.apply(pessoa1, [4]));
18+
return result;
19+
}

0 commit comments

Comments
 (0)