Skip to content

Commit f3d3946

Browse files
committed
async
1 parent aa5d9fe commit f3d3946

File tree

3 files changed

+165
-1
lines changed

3 files changed

+165
-1
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width,initial-scale=1.0">
7+
<title>Async Await</title>
8+
<link rel="stylesheet" href="../base.css">
9+
</head>
10+
11+
<body>
12+
<script>
13+
function wait(ms = 0) {
14+
return new Promise((resolve) => {
15+
setTimeout(resolve, ms);
16+
})
17+
}
18+
19+
function makePizza(toppings = []) {
20+
return new Promise(function (resolve, reject) {
21+
// reject if people try with pineapple
22+
if (toppings.includes('pineapple')) {
23+
reject('Seriously? Get out 🍍');
24+
}
25+
const amountOfTimeToBake = 500 + (toppings.length * 200);
26+
// wait 1 second for the pizza to cook:
27+
setTimeout(function () {
28+
// when you are ready, you can resolve this promise
29+
resolve(`Here is your pizza 🍕 with the toppings ${toppings.join(' ')}`);
30+
}, amountOfTimeToBake);
31+
// if something went wrong, we can reject this promise;
32+
});
33+
}
34+
35+
function handleError(err) {
36+
console.log('ohhh noooo');
37+
console.log(err);
38+
}
39+
40+
function handleDisgustingPizza() {
41+
42+
}
43+
async function go() {
44+
const pizza = await makePizza(['pineapple']).catch(handleDisgustingPizza);
45+
return pizza;
46+
}
47+
48+
// catch it at run time
49+
go().catch(handleError);
50+
// make a safe function with a HOF
51+
function makeSafe(fn, errorHandler) {
52+
return function () {
53+
fn().catch(errorHandler)
54+
}
55+
}
56+
57+
const safeGo = makeSafe(go, handleError);
58+
59+
safeGo();
60+
61+
</script>
62+
</body>
63+
64+
</html>

playground/async-await.html

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width,initial-scale=1.0">
7+
<title>Async Await</title>
8+
<link rel="stylesheet" href="../base.css">
9+
</head>
10+
11+
<body>
12+
<script>
13+
function wait(ms = 0) {
14+
return new Promise((resolve) => {
15+
setTimeout(resolve, ms);
16+
})
17+
}
18+
19+
function makePizza(toppings = []) {
20+
return new Promise(function (resolve, reject) {
21+
// reject if people try with pineapple
22+
if (toppings.includes('pineapple')) {
23+
reject('Seriously? Get out 🍍');
24+
}
25+
const amountOfTimeToBake = 500 + (toppings.length * 200);
26+
// wait 1 second for the pizza to cook:
27+
setTimeout(function () {
28+
// when you are ready, you can resolve this promise
29+
resolve(`Here is your pizza 🍕 with the toppings ${toppings.join(' ')}`);
30+
}, amountOfTimeToBake);
31+
// if something went wrong, we can reject this promise;
32+
});
33+
}
34+
35+
async function go() {
36+
console.log('Starting');
37+
await wait(2000);
38+
console.log('running');
39+
await wait(200);
40+
console.log('ending');
41+
}
42+
43+
// go();
44+
45+
async function makeDinner() {
46+
const pizzaPromise1 = makePizza(['pepperoni']);
47+
const pizzaPromise2 = makePizza(['mushrooms']);
48+
const [pep, mush] = await Promise.all([pizzaPromise1, pizzaPromise2]);
49+
console.log(pep, mush);
50+
}
51+
52+
makeDinner();
53+
54+
// // Function declaration
55+
// async function fd() { }
56+
57+
// // arrow function
58+
// const arrowFn = async () => { }
59+
60+
// // call back
61+
// window.addEventListener('click', async function () {
62+
63+
// })
64+
65+
// //
66+
// const person = {
67+
// // method
68+
// sayHi: async function () {
69+
70+
// },
71+
// // method shorthand
72+
// async sayHello() {
73+
74+
// },
75+
// // function property
76+
// sayHey: async () => {
77+
78+
// }
79+
// }
80+
</script>
81+
</body>
82+
83+
</html>

playground/promise-chain-FINISHED.html

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,23 @@
4646

4747
const go = document.querySelector('.go');
4848

49+
async function animate2(e) {
50+
const el = e.currentTarget;
51+
// 1. Change the text to GO when clicked.
52+
el.textContent = 'GO';
53+
// 2. Make it a circle after 2 seconds
54+
await wait(200);
55+
el.classList.add('circle');
56+
await wait(500);
57+
el.classList.add('red');
58+
await wait(250);
59+
el.classList.remove('circle');
60+
await wait(500);
61+
el.classList.remove('red');
62+
el.classList.add('purple');
63+
await wait(500);
64+
el.classList.add('fadeOut');
65+
}
4966

5067
function animate(e) {
5168
const el = e.currentTarget;
@@ -76,7 +93,7 @@
7693
})
7794
}
7895

79-
go.addEventListener('click', animate);
96+
go.addEventListener('click', animate2);
8097

8198
go.addEventListener('clickXXXX', function go(e) {
8299
const el = e.currentTarget;

0 commit comments

Comments
 (0)